Code Monkey home page Code Monkey logo

is-base64's Introduction

is-base64

Predicate that returns true if base64 string.

Install

npm install is-base64

Usage

var isBase64 = require('is-base64');

var string = 'iVBORw0KGgoAAAAN ... kSuQmCC';
var stringWithMime = 'data:image/png;base64,iVBORw0KGgoAAAA ... AAElFTkSuQmCC';

console.log(isBase64(string)); // true
console.log(isBase64(stringWithMime)); // false
console.log(isBase64(stringWithMime, {allowMime: true})); // true
console.log(isBase64(string, {mimeRequired: true})); // false
console.log(isBase64(stringWithMime, {mimeRequired: true})); // true
console.log(isBase64('1342234')); // false
console.log(isBase64('afQ$%rfew')); // false
console.log(isBase64('dfasdfr342')); // false
console.log(isBase64('uuLMhh==')); // true
console.log(isBase64('uuLMhh')); // false
console.log(isBase64('uuLMhh', {paddingRequired: false})); // true
console.log(isBase64('')); // true
console.log(isBase64('', {allowEmpty: false})); // false

API

isBase64(string, options)

  • {string} string - string to check if is valid base64 string

  • {object} [options]

    • [options.allowEmpty=true] {boolean} - returns true for empty string
    • [options.allowMime=false] {boolean} - returns true for valid strings with optional mime
    • [options.mimeRequired=false] {boolean} - returns true for valid strings with mime
    • [options.paddingRequired=true] {boolean} - returns true for valid strings with valid padding

CLI

Install CLI:

npm install -g is-base64

CLI example:

$ is-base64 aGVsbG8gd29ybGQ=
true

Piping example:

$ echo aGVsbG8gd29ybGQ= | is-base64
true

FAQ

  • Q: Why is empty string "" a valid base64 string by default?

License

MIT

is-base64's People

Contributors

miguelmota avatar renarsvilnis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

is-base64's Issues

Option to detect if string is base64 without mime

Hi @miguelmota!

Sometimes there are cases when we can easily handle datauri strings

var img = new Image(); img.src = dataUriString

but we cannot that easily handle raw base64 string, we have to convert them to datauri via create-data-uri or alike.

That would be nice to have an option to detect only base64 string without mime type:

isBase64('data:image/png;base64,iVBORw0KGgoAAAA ... AAElFTkSuQmCC') // true
isBase64('data:image/png;base64,iVBORw0KGgoAAAA ... AAElFTkSuQmCC', {mime: false}) // false

Doesn't work on string "test"

I've noticed that testing the string "test" will report true:

isBase64('test');
// => true

The same is true for other combinations of the same letters, such as "estt", "sett" etc.

Bug with DELL word

When i test if DELL word is a base64 string, your function returns true...

Corner case

Hi @miguelmota!
Just stumbled upon

isBase64('uuLMhh') //false

Although that โ†‘ should be true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.