split off from uci-utils - see uci-utils for former commits

master
David Kebler 2019-02-16 11:45:22 -08:00
commit 9b8e0525ef
7 changed files with 132 additions and 0 deletions

37
.eslintrc.js Normal file
View File

@ -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"
]
}
}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/node_modules/
/coverage/

4
.npmignore Normal file
View File

@ -0,0 +1,4 @@
tests/
test/
*.test.js
testing/

39
package.json Normal file
View File

@ -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"
}
}

3
readme.md Normal file
View File

@ -0,0 +1,3 @@
### uCOMmandIt Type Library
Extension of TypeChecker Package

27
src/type.js Normal file
View File

@ -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 }

20
test/type.test.js Normal file
View File

@ -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')
})
})