Skip to content

Latest commit

 

History

History
169 lines (119 loc) · 5.43 KB

README.md

File metadata and controls

169 lines (119 loc) · 5.43 KB

type

Bulletproof type checking for JavaScript. Think of it as an expanded version of the native typeof function. The module recognizes types that typeof does but also many more that typeof clunks together in a less useful way.

🧭 Table of contents

✨ Benefits

  • recognizes native JS types in more detail
  • recognizes custom object types

🎒 Requierments

There are no special prerequisites.

🚀 Quickstart

Install

NodeJS:

npm install @danielcobo/type

If you are using a browser you can declare it as a global variable named typeOf by including this script before any script you want to use it in:

<script src="https://cdn.jsdelivr.net/npm/@danielcobo/type@1/dist/iife/typeof.min.js"></script>

Or you can import it inside your JS file as an ECMAScript module:

import * as typeOf from 'https://cdn.jsdelivr.net/npm/@danielcobo/type@1/dist/esm/typeof.min.js';

Also, feel free to download the file if you prefer not to use jsdelivr. In that case just replace the url with the relative file path.

Note: In case you're wondering, @danielcobo/ is just a namespace scope - an NPM feature. Scopes make it easier to name modules and improve security.

Usage example

const bestTimeToStart = new Date()
typeOf(bestTimeToStart);  //date

For details see documentation below.

📘 Documentation

Returns type name of input value.

Param Type Description
val Any Input value
Returns Type Description
type string Type of the input value

Types that are part of the native typeof function

typeOf(1n);              //bigint
typeOf(true);            //boolean
typeOf(false);           //boolean
typeOf(function () {});  //function
typeOf(1);               //number
typeOf(0.1);             //number
typeOf('hello string');  //string

const noDefinition = undefined;
let noDefinitionEither;
typeOf(noDefinition);        //undefined
typeOf(noDefinitionEither);  //undefined
typeOf());                   //undefined
typeOf(undefined);           //undefined

Types that are NOT part of the native typeof function

typeOf(arguments);          //arguments
typeOf([]);                 //array
typeOf(Buffer.alloc(43));   //buffer
typeOf(new Date());         //date
typeOf(NaN);                //NaN
typeOf(null);               //null
typeOf(/regex/);            //regex

Custom types

Custom object classes will give you custom types.

class Robot {
  constructor(name, side) {
    this.name = name;
    this.side = side;
  }
}
const metroplex = new Robot('Metroplex', 'autobot');
typeOf(metroplex); //robot

🆘 Troubleshooting

If you run into trouble or have questions just submit an issue.

🤝 Contributing

Anyone can contribute

Contributions come in many shapes and sizes. All are welcome. You can contribute by:

  • asking questions
  • suggesting features
  • sharing this repo with friends
  • improving documentation (even fixing typos counts 😉)
  • providing tutorials (if you do, please let me know, I would love to read them)
  • improving tests
  • contributing code (new features, performance boosts, code readability improvements..)

Rules for contributions

General guidelines:

  • there are no dumb questions
  • be polite and respectful to others
  • do good

When coding remember:

  • working > maintainability > performance
  • best code is no code
  • be descriptive when naming
  • keep it DRY
  • do test

Contribution licence: All contributions are considered to be under same license as this repository.

🧪 Testing

Testing suite: 🃏 Jest | Test command: npm test

Mutation testing suite: 👽 Stryker Mutator | Mutation test command: npm run mutate

If you intend to develop further or contribute code, then please ensure to write and use testing. Strive for 100% code coverage and high mutation scores. Mutation score 100 is great, but it's not always neccessary (if there are valid reasons).

⚖️ License

MIT License