Skip to content

Commit

Permalink
feat: support es modules
Browse files Browse the repository at this point in the history
  • Loading branch information
scopewu committed Apr 15, 2023
1 parent 7e1a987 commit 7a59466
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# type
data types in javascript
# JavaScript type

Use `Object.propotype.toString.call` function to Check the type of JavaScript value.
60 changes: 41 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const toString = Object.prototype.toString;

/**
* get `toString.call()` result
* @param value {*} The value to call `toString.call()`
* @returns {string|string} Return `toString.call()` result
*/
function getTag(value) {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]'
return value === undefined ? '[object Undefined]' : '[object Null]';
}
return toString.call(value);
}
Expand All @@ -19,14 +24,22 @@ function getTag(value) {
* @param value {*} The value to get class name
* @return {string} Return [[Class]] name
*/
function getClass(value) {
export function getClass(value) {
return /^\[object (.*)]$/.exec(getTag(value))[1];
}

/**
* generator isType function
* @param type {string} The type name
* @returns {function(*): boolean}
*/
function isType(type) {
const cls = '[object ' + type + ']';
/**
* Check if value is a type
*/
return function (value) {
return getTag(value) === cls
return getTag(value) === cls;
}
}

Expand All @@ -35,30 +48,30 @@ function isType(type) {
* @param value {*} The value to check
* @return {boolean} Return true if value is a date
*/
const isDate = isType('Date');
export 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');
export 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')
export 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) {
export const isNumber = function (value) {
return typeof value === 'number' || isType('Number');
}

Expand All @@ -67,7 +80,7 @@ const isNumber = function (value) {
* @param value {*} The value to check
* @return {boolean} Return true if value is a string
*/
const isString = function (value) {
export const isString = function (value) {
return typeof value === 'string' || isType('String');
}

Expand All @@ -76,35 +89,44 @@ const isString = function (value) {
* @param value {*} The value to check
* @return {boolean} Return true if value is a function
*/
const isFunction = function (value) {
export const isFunction = function (value) {
return typeof value === 'function' || isType('Function');
}

/**
* Check if value is a Array object
* Check if value is an Array object
* @param value {*} The value to check
* @return {boolean} Return true if value is a array
*/
const isArray = Array.isArray || isType('Array');
export 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)
export 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
export function isDefined(value) {
return value != null;
}




export default {
getClass,
isDate,
isRegExp,
isBoolean,
isNumber,
isString,
isFunction,
isArray,
isNaN,
isDefined
}
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
{
"name": "type",
"name": "types",
"version": "1.0.0",
"description": "Check data types in javascript",
"description": "Check the type of JavaScript value",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/scopewu/type.git"
"url": "git+https://github.com/scopewu/types.git"
},
"keywords": [
"type",
"types in javascript",
"check type",
"isType"
"isType",
"isDate",
"isNumber",
"isRegExp"
],
"author": "scope.wu <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/scopewu/type/issues"
"url": "https://github.com/scopewu/types/issues"
},
"homepage": "https://github.com/scopewu/type#readme"
"homepage": "https://github.com/scopewu/types#readme"
}

0 comments on commit 7a59466

Please sign in to comment.