From 7e1a98773fbe0dc9dfdede5f243676cda4885e15 Mon Sep 17 00:00:00 2001 From: scopewu Date: Sun, 16 Jun 2019 11:18:52 +0800 Subject: [PATCH] feat: is type --- .editorconfig | 15 +++++++ .gitignore | 70 ++++++++++++++++++++++++++++++++ index.js | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 ++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..82da0b1 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +insert_final_newline = false +trim_trailing_whitespace = false \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f46b4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,70 @@ + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Coverage directory used by tools like istanbul +coverage + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +### OSX ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +### VisualStudioCode ### +.vscode/ + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history + +### WebStorm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +.idea/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..f662577 --- /dev/null +++ b/index.js @@ -0,0 +1,110 @@ +const toString = Object.prototype.toString; + +function getTag(value) { + if (value == null) { + return value === undefined ? '[object Undefined]' : '[object Null]' + } + return toString.call(value); +} + +/** + * get [[Class]] name + * + * getClass('') + * // => 'String' + * + * getClass(new Date()) + * // => 'Date' + * + * @param value {*} The value to get class name + * @return {string} Return [[Class]] name + */ +function getClass(value) { + return /^\[object (.*)]$/.exec(getTag(value))[1]; +} + +function isType(type) { + const cls = '[object ' + type + ']'; + return function (value) { + return getTag(value) === cls + } +} + +/** + * Check if value is a Date object + * @param value {*} The value to check + * @return {boolean} Return true if value is a date + */ +const isDate = isType('Date'); + +/** + * Check if value is a regexp + * @param value {*} The value to check + * @return {boolean} Return true if value is a regexp + */ +const isRegExp = isType('RegExp'); + +/** + * Check if value is a bool value + * @param value {*} The value to check + * @return {boolean} Return true if value is a bool + */ +const isBoolean = function (value) { + return value === true || value === false || isType('Boolean') +} + +/** + * Check if value is a Number + * @param value {*} The value to check + * @return {boolean} Return true if value is a number + */ +const isNumber = function (value) { + return typeof value === 'number' || isType('Number'); +} + +/** + * Check if value is a string + * @param value {*} The value to check + * @return {boolean} Return true if value is a string + */ +const isString = function (value) { + return typeof value === 'string' || isType('String'); +} + +/** + * Check if value is a Function object + * @param value {*} The value to check + * @return {boolean} Return true if value is a function + */ +const isFunction = function (value) { + return typeof value === 'function' || isType('Function'); +} + +/** + * Check if value is a Array object + * @param value {*} The value to check + * @return {boolean} Return true if value is a array + */ +const isArray = Array.isArray || isType('Array'); + +/** + * Check if value is NaN + * @param value {*} The value of check + * @return {boolean} Return true if value is NaN + */ +const isNaN = Number.isNaN || function(value) { + return typeof value === 'number' || isNaN(value) +} + +/** + * Check if value is not null or undefined + * @param value {*} The value to check + * @return {boolean} Return true if value is not null or undefined + */ +function isDefined(value) { + return value != null +} + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..26ca08e --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "type", + "version": "1.0.0", + "description": "Check data types in javascript", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/scopewu/type.git" + }, + "keywords": [ + "type", + "types in javascript", + "check type", + "isType" + ], + "author": "scope.wu ", + "license": "MIT", + "bugs": { + "url": "https://github.com/scopewu/type/issues" + }, + "homepage": "https://github.com/scopewu/type#readme" +}