Coder Social home page Coder Social logo

whether's Introduction

Whether

identify file type by file magic numbers.

about magic numbers

  • Magic numbers are the first bits of a file which uniquely identify the type of file.
  • see details.

Getting start

install

  npm install whether

check an image is exactly the given type.

  
  var whether = require('whether').create();

  whether('path/to/image').is('jpg'); // will return boolean

  // or async way
  whether('path/to/image').is('jpg', callback);

  // check whether the file content type is matched with the file extension.
  whether('path/to/image.jpg').isMatched(); //matched with jpg type, return boolean.

  // or aync way
  whether('path/to/image.jpg').isMatched(callback);
  // isValid is an alias
  whether('path/to/image.jpg').isValid(callback);

plain identify

identify a file with magic numbers. keep in mind the identify is a class method.

  
  var Whether = require('whether');

  // identify jpg file, will return boolean
  Whether.identify('path/to/image', 'FFD8FF'); 

  // async way
  Whether.identify('path/to/image', 'FFD8FF', callback);  

exts map defines

see the config.js

you can define your map.

  
  // will only overwrite the exist keys
  var whether = require('whether')({
    exts: {
      'ext1': '...',
      'ext2': '...',
      'ext3': '...'
    }
  });

  // then use these definitions.
  whether('path/to/image').is('ext1');
  whether('path/to/image.ext2').isMatched();

define file type check methods

sometimes, we want to check a file is some kind of file.

eg: whether a file is an image.

define a check method, use an array to tell which exts are included.

  
  // define in config.
  var whether = require('whether')({
    defs: {
      isImage: ['png', 'gif', 'jpg', 'bmp'],
      isMS: ['doc', 'ppt', 'xls'],
      ...
    },
    exts: {...}
  });

  // use the defined methods
  whether('path/to/image').isImage();
  // async way
  whether('path/to/image').isImage(callback);

  // define instantly
  var whether = require('whether')({
    exts: {
      ext1: '...',
      ext2: '...'
    }
  });
  whether.define('isSomething', ['ext1', 'ext2']);
  // then use
  whether('path/to/file').isSomething();
  whether('path/to/file').isSomething(callback);

notice: all the exts used in methods definitions should have defined in the config.exts

details

please see the test files.

license

MIT

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.