diff --git a/LICENSE b/LICENSE index d0eb666..fa685e1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Codevor - js-library-boilerplate +Copyright (c) 2019 Codevor - js-is-type Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index dcbb341..ef9bf4f 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,27 @@ -# js-library-boilerplate +# 🎯Type-checking for 'Primitive' JS Types [![License][license-badge]][license-url] [![Travis CI][travis-badge]][travis-url] [![Coverage Status][coverage-badge]][coverage-url] [![Commitizen][commitizen-badge]][commitizen-url] -> js-library-boilerplate-description. +Type-check for 'Primitive' types in JavaScript are always something to take care when receiving/sending a payload from/to Server/Client application. If you ever wonder how to do this you can sure use `typeof` and make the check by yourself. Or you can user 🎯`js-is-type`! + +The usage is simple and verbose. The tests has full coverage and you can use without a doubt. + +## Available Types + +- `isArray()` +- `isBoolean()` +- `isFunction()` +- `isObject()` (_Remember: in JS, Arrays are Objects!_) +- `isString()` +- `isUndefined()` ## Installation -js-library-boilerplate is available on npm/yarn: +`js-is-type` is available on npm/yarn: ```bash -$ npm install js-library-boilerplate --save -$ yarn add js-library-boilerplate +$ npm install @codevor/js-is-type --save +$ yarn add @codevor/js-is-type ``` ## Usage @@ -18,17 +29,21 @@ $ yarn add js-library-boilerplate ### With ES6/import ```js -import { sum } from 'js-library-boilerplate'; +import { isArray } from '@codevor/js-is-type'; -sum(2, 2); // => 4 +const names = ['Alice', 'Bob']; + +isArray(names); // => true ``` ### With require ```js -const sum = require('js-library-boilerplate').sum; +const isArray = require('@codevor/js-is-type').isArray; + +const names = ['Alice', 'Bob']; -sum(2, 2); // => 4 +isArray(names); // => true ``` ## Contributing @@ -37,21 +52,21 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduc ## Changelog -This project adheres to [Semantic Versioning](https://semver.org/). Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/codevor/js-library-boilerplate/releases) page. +This project adheres to [Semantic Versioning](https://semver.org/). Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/codevor/js-is-type/releases) page. ## Bugs and Sugestions -Report bugs or do suggestions using the [issues](https://github.com/codevor/js-library-boilerplate/issues). +Report bugs or do suggestions using the [issues](https://github.com/codevor/js-is-type/issues). ## License [MIT License](LICENSE) © [Codevor](https://github.com/codevor) -[license-badge]: https://img.shields.io/github/license/codevor/js-library-boilerplate.svg +[license-badge]: https://img.shields.io/github/license/codevor/js-is-type.svg [license-url]: https://opensource.org/licenses/MIT -[coverage-badge]: https://coveralls.io/repos/github/codevor/js-library-boilerplate/badge.svg?branch=master -[coverage-url]: https://coveralls.io/github/codevor/js-library-boilerplate?branch=master -[travis-badge]: https://travis-ci.org/codevor/js-library-boilerplate.svg?branch=master -[travis-url]: https://travis-ci.org/codevor/js-library-boilerplate +[coverage-badge]: https://coveralls.io/repos/github/codevor/js-is-type/badge.svg?branch=master +[coverage-url]: https://coveralls.io/github/codevor/js-is-type?branch=master +[travis-badge]: https://travis-ci.org/codevor/js-is-type.svg?branch=master +[travis-url]: https://travis-ci.org/codevor/js-is-type [commitizen-badge]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg [commitizen-url]: http://commitizen.github.io/cz-cli/ diff --git a/package.json b/package.json index b90ed61..bc2c247 100644 --- a/package.json +++ b/package.json @@ -1,31 +1,34 @@ { - "name": "js-library-boilerplate", + "name": "@codevor/js-is-type", "version": "0.1.0", - "description": "js-library-boilerplate-description", - "main": "dist/js-library-boilerplate.js", - "unpkg": "dist/js-library-boilerplate.min.js", + "description": "🎯Type-checking for 'Primitive' JS Types made easy!", + "main": "dist/js-is-type.js", + "unpkg": "dist/js-is-type.min.js", "scripts": { "clean": "rimraf dist", "dev": "NODE_ENV=dev webpack --progress --colors --watch", "build:umd": "NODE_ENV=production webpack", "lint": "eslint src tests", - "test": "jest --coverage --expand", + "test": "jest --coverage", "test:watch": "jest --watch", "coveralls": "cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", "prepublish": "yarn lint && yarn test && yarn clean && yarn build:umd", "commit": "git-cz" }, - "keywords": [], - "author": "Helder Burato Berto (https://helder.dev/)", + "keywords": [ + "Utils", + "Typechecking" + ], + "author": "Ilê Caian (https://ile.pink/)", "license": "MIT", "repository": { "type": "git", - "url": "git+https://github.com/codevor/js-library-boilerplate.git" + "url": "git+https://github.com/codevor/js-is-type.git" }, "bugs": { - "url": "https://github.com/codevor/js-library-boilerplate/issues" + "url": "https://github.com/codevor/js-is-type/issues" }, - "homepage": "https://github.com/codevor/js-library-boilerplate#readme", + "homepage": "https://github.com/codevor/js-is-type#readme", "devDependencies": { "@babel/cli": "^7.6.4", "@babel/core": "^7.6.4", diff --git a/src/index.js b/src/index.js index 12ee0dd..011b41e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1 @@ -export function sum(x, y) { - return x + y; -} +export * from './validators'; diff --git a/src/validators/index.js b/src/validators/index.js new file mode 100644 index 0000000..acf9ffb --- /dev/null +++ b/src/validators/index.js @@ -0,0 +1,6 @@ +export { default as isArray } from './is-array'; +export { default as isBoolean } from './is-boolean'; +export { default as isFunction } from './is-function'; +export { default as isObject } from './is-object'; +export { default as isString } from './is-string'; +export { default as isUndefined } from './is-undefined'; diff --git a/src/validators/is-array.js b/src/validators/is-array.js new file mode 100644 index 0000000..468864c --- /dev/null +++ b/src/validators/is-array.js @@ -0,0 +1,3 @@ +const isArray = value => Array.isArray(value); + +export default isArray; diff --git a/src/validators/is-boolean.js b/src/validators/is-boolean.js new file mode 100644 index 0000000..74b998e --- /dev/null +++ b/src/validators/is-boolean.js @@ -0,0 +1,3 @@ +const isBoolean = value => typeof value === 'boolean'; + +export default isBoolean; diff --git a/src/validators/is-function.js b/src/validators/is-function.js new file mode 100644 index 0000000..0b3cc34 --- /dev/null +++ b/src/validators/is-function.js @@ -0,0 +1,3 @@ +const isFunction = value => typeof value === "function"; + +export default isFunction; diff --git a/src/validators/is-object.js b/src/validators/is-object.js new file mode 100644 index 0000000..dd564ec --- /dev/null +++ b/src/validators/is-object.js @@ -0,0 +1,3 @@ +const isObject = value => typeof value === "object"; + +export default isObject; diff --git a/src/validators/is-string.js b/src/validators/is-string.js new file mode 100644 index 0000000..7b55718 --- /dev/null +++ b/src/validators/is-string.js @@ -0,0 +1,3 @@ +const isString = value => typeof value === "string"; + +export default isString; diff --git a/src/validators/is-undefined.js b/src/validators/is-undefined.js new file mode 100644 index 0000000..ac794d6 --- /dev/null +++ b/src/validators/is-undefined.js @@ -0,0 +1,3 @@ +const isUndefined = value => typeof value === "undefined"; + +export default isUndefined; diff --git a/tests/is-array.test.js b/tests/is-array.test.js new file mode 100644 index 0000000..d063f8a --- /dev/null +++ b/tests/is-array.test.js @@ -0,0 +1,34 @@ +import { isArray } from '../src'; + +describe('isArray()', () => { + const anonymousFunction = () => {}; + function namedFunction() {} + const emptyArrayValue = []; + const arrayValue = [2, 8, 'test']; + const stringValue = 'This is a string'; + const booleanValue = true; + + test('empty array should be valid', () => { + expect(isArray(emptyArrayValue)).toBeTruthy(); + }); + + test('correct array, not empty should be valid', () => { + expect(isArray(arrayValue)).toBeTruthy(); + }); + + test('anonymous function should be invalid', () => { + expect(isArray(anonymousFunction)).toBeFalsy(); + }); + + test('named function should be invalid', () => { + expect(isArray(namedFunction)).toBeFalsy(); + }); + + test('non array (string value) should be invalid', () => { + expect(isArray(stringValue)).toBeFalsy(); + }); + + test('non array (boolean value) should be invalid', () => { + expect(isArray(booleanValue)).toBeFalsy(); + }); +}); diff --git a/tests/is-boolean.test.js b/tests/is-boolean.test.js new file mode 100644 index 0000000..15ed93e --- /dev/null +++ b/tests/is-boolean.test.js @@ -0,0 +1,34 @@ +import { isBoolean } from '../src'; + +describe('isBoolean()', () => { + const anonymousFunction = () => {}; + function namedFunction() {}; + const booleanValue = true; + const arrayValue = [4, true, 'test']; + const stringValue = 'This is a string'; + const numberValue = 3; + + test('boolean value should be valid', () => { + expect(isBoolean(booleanValue)).toBeTruthy(); + }); + + test('anonymous function value should be invalid', () => { + expect(isBoolean(anonymousFunction)).toBeFalsy(); + }); + + test('named function value should be invalid', () => { + expect(isBoolean(namedFunction)).toBeFalsy(); + }); + + test('array value should be invalid', () => { + expect(isBoolean(arrayValue)).toBeFalsy(); + }); + + test('string value should be invalid', () => { + expect(isBoolean(stringValue)).toBeFalsy(); + }); + + test('number value should be invalid', () => { + expect(isBoolean(numberValue)).toBeFalsy(); + }); +}); diff --git a/tests/is-function.test.js b/tests/is-function.test.js new file mode 100644 index 0000000..821db90 --- /dev/null +++ b/tests/is-function.test.js @@ -0,0 +1,34 @@ +import { isFunction } from '../src'; + +describe('isFunction()', () => { + const anonymousFunction = () => {}; + function namedFunction() {} + const booleanValue = true; + const arrayValue = [4, true, 'test']; + const stringValue = 'This is a string'; + const numberValue = 3; + + test('anonymous function should be valid', () => { + expect(isFunction(anonymousFunction)).toBeTruthy(); + }); + + test('named function should be valid', () => { + expect(isFunction(namedFunction)).toBeTruthy(); + }); + + test('boolean value should be invalid', () => { + expect(isFunction(booleanValue)).toBeFalsy(); + }); + + test('array value should be invalid', () => { + expect(isFunction(arrayValue)).toBeFalsy(); + }); + + test('string value should be invalid', () => { + expect(isFunction(stringValue)).toBeFalsy(); + }); + + test('number value should be invalid', () => { + expect(isFunction(numberValue)).toBeFalsy(); + }); +}); diff --git a/tests/is-object.test.js b/tests/is-object.test.js new file mode 100644 index 0000000..8b72fb1 --- /dev/null +++ b/tests/is-object.test.js @@ -0,0 +1,34 @@ +import { isObject } from '../src'; + +describe('isObject()', () => { + const anonymousFunction = () => {}; + function namedFunction() {} + const objectValue = { test: 1 }; + const arrayValue = []; + const numberValue = 3; + const booleanValue = true; + + test('object value should be valid', () => { + expect(isObject(objectValue)).toBeTruthy(); + }); + + test('array value should be valid(arrays are objects!!)', () => { + expect(isObject(arrayValue)).toBeTruthy(); + }); + + test('anonymous function value should be invalid', () => { + expect(isObject(anonymousFunction)).toBeFalsy(); + }); + + test('named function value should be invalid', () => { + expect(isObject(namedFunction)).toBeFalsy(); + }); + + test('number value should be invalid', () => { + expect(isObject(numberValue)).toBeFalsy(); + }); + + test('boolean value should be invalid', () => { + expect(isObject(booleanValue)).toBeFalsy(); + }); +}); diff --git a/tests/is-string.test.js b/tests/is-string.test.js new file mode 100644 index 0000000..c902849 --- /dev/null +++ b/tests/is-string.test.js @@ -0,0 +1,39 @@ +import { isString } from '../src'; + +describe('isString()', () => { + const anonymousFunction = () => {}; + function namedFunction() {} + const objectValue = { test: 1 }; + const arrayValue = []; + const stringValue = 'defined'; + const numberValue = 3; + const booleanValue = true; + + test('string value should be valid', () => { + expect(isString(stringValue)).toBeTruthy(); + }); + + test('anonymous function should be invalid', () => { + expect(isString(anonymousFunction)).toBeFalsy(); + }); + + test('named function should be invalid', () => { + expect(isString(namedFunction)).toBeFalsy(); + }); + + test('object value should be invalid', () => { + expect(isString(objectValue)).toBeFalsy(); + }); + + test('array value should be invalid', () => { + expect(isString(arrayValue)).toBeFalsy(); + }); + + test('number value should be invalid', () => { + expect(isString(numberValue)).toBeFalsy(); + }); + + test('boolean value should be invalid', () => { + expect(isString(booleanValue)).toBeFalsy(); + }); +}); diff --git a/tests/is-undefined.test.js b/tests/is-undefined.test.js new file mode 100644 index 0000000..c7de4bc --- /dev/null +++ b/tests/is-undefined.test.js @@ -0,0 +1,14 @@ +import { isUndefined } from '../src'; + +describe('isUndefined()', () => { + const definedValue = 2; + const notDefinedValue = undefined; + + test('defined value should be invalid', () => { + expect(isUndefined(definedValue)).toBeFalsy(); + }); + + test('non defined should be valid', () => { + expect(isUndefined(notDefinedValue)).toBeTruthy(); + }); +}); diff --git a/tests/sum.test.js b/tests/sum.test.js deleted file mode 100644 index 6196d62..0000000 --- a/tests/sum.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import { sum } from '../src'; - -describe('sum', () => { - test('it should sum 2 + 2', () => { - expect(sum(2, 2)).toBe(4); - }); -}); diff --git a/webpack.config.js b/webpack.config.js index e057bb4..4aa34a6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,7 +5,7 @@ const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); const isProduction = process.env.NODE_ENV === 'production'; const mode = isProduction ? 'production' : 'development'; -const libraryName = 'js-library-boilerplate'; +const libraryName = 'js-is-type'; module.exports = { mode,