commit 9b8e0525efda851c8e64c61c4fbf8a642d892283 Author: David Kebler Date: Sat Feb 16 11:45:22 2019 -0800 split off from uci-utils - see uci-utils for former commits diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..49bac18 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,37 @@ +module.exports = { + "ecmaFeatures": { + "modules": true, + "spread" : true, + "restParams" : true + }, + // "plugins": [ + // "unicorn" + // ], + "env": { + "es6": true, + "node": true, + "mocha": true + }, + "parserOptions": { + "ecmaVersion": 2017, + "sourceType": "module" + }, + "extends": "eslint:recommended", + "rules": { + "indent": [ + "error", + 2 + ], + // "unicorn/no-array-instanceof": "error", + "no-console": 0, + "semi": ["error", "never"], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e61051f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/coverage/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..f16fc41 --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +tests/ +test/ +*.test.js +testing/ diff --git a/package.json b/package.json new file mode 100644 index 0000000..12b8d88 --- /dev/null +++ b/package.json @@ -0,0 +1,39 @@ +{ + "name": "@uci-utils/type", + "version": "0.2.0", + "description": "A variable type check utility - extentsion of TypeChecker package", + "main": "src/type.js", + "scripts": { + "test": "./node_modules/.bin/mocha -r esm --timeout 30000", + "testd": "UCI_ENV=dev ./node_modules/.bin/nodemon --exec './node_modules/.bin/mocha -r esm --timeout 30000' || exit 0", + "testdd": "UCI_LOG_LEVEL='trace' npm run testd", + "testde": "UCI_LOG_LEVEL='warn' npm run testd", + "testl": "UCI_ENV=pro UCI_LOG_PATH=./test/test.log 0 npm run test || exit 0" + }, + "author": "David Kebler", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/uCOMmandIt/uci-utils.git" + }, + "keywords": [ + "node.js", + "type", + "typeof", + "typechecker", + "variables" + ], + "bugs": { + "url": "https://github.com/uCOMmandIt/uci-utils/issues" + }, + "homepage": "https://github.com/uCOMmandIt/uci-utils#readme", + "dependencies": { + "typechecker": "^4.5.0" + }, + "devDependencies": { + "chai": "^4.2.0", + "esm": "^3.2.4", + "mocha": "^5.2.0", + "nodemon": "^1.18.10" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..c37ff89 --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +### uCOMmandIt Type Library + +Extension of TypeChecker Package diff --git a/src/type.js b/src/type.js new file mode 100644 index 0000000..795f171 --- /dev/null +++ b/src/type.js @@ -0,0 +1,27 @@ +import typechecker from 'typechecker' + +const customTypeChecker = Object.assign({}, typechecker) // typechecker is frozen + +// add custom types +customTypeChecker.isPromise = function isPromise(obj) { + return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function' +} + +customTypeChecker.isBuffer = function isBuffer(obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) +} + +// Add custom types to typeMap +let customTypeMap = { + buffer: customTypeChecker.isBuffer, + promise: customTypeChecker.isPromise +} + +customTypeMap = Object.assign(customTypeMap, customTypeChecker.typeMap) + +customTypeChecker.getType = function customGetType(value, _typeMap) { + return typechecker.getType(value, _typeMap || customTypeMap) +} + +export default customTypeChecker +export { customTypeChecker as type } diff --git a/test/type.test.js b/test/type.test.js new file mode 100644 index 0000000..0d81533 --- /dev/null +++ b/test/type.test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import u from '../src/type' + +describe('Variable Types Library - ', function () { + + it('Should include custom types', function () { + expect(u.isBuffer(Buffer.from('this is a test'))).to.equal(true) + expect(u.getType(Buffer.from('this is a test'))).to.equal('buffer') + expect(u.isPromise(Promise.resolve(2))).to.equal(true) + expect(u.getType(Promise.resolve(2))).to.equal('promise') + }) + + it('Should load typechecker', function () { + expect(u.isPlainObject([1])).to.equal(false) + expect(u.getType(true)).to.equal('boolean') + expect(u.getObjectType('a string')).to.equal('[object String]') + expect(u.getType('this is a test')).to.equal('string') + }) + +})