npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

whether

v0.1.1

Published

identify file type by file magic numbers.

Downloads

128

Readme

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