Skip to content

Commit

Permalink
feat: is type
Browse files Browse the repository at this point in the history
  • Loading branch information
scopewu committed Jun 16, 2019
1 parent 1e8cdd5 commit 7e1a987
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
110 changes: 110 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
}




25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/scopewu/type/issues"
},
"homepage": "https://github.com/scopewu/type#readme"
}

0 comments on commit 7e1a987

Please sign in to comment.