Skip to content

Commit

Permalink
feat: remove lots of is* funcs, put these gather in is.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
suressk committed Jun 28, 2022
1 parent 6004961 commit e00f8cf
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 203 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ Using `npm`, `yarn` or `pnpm`

```shell
npm i sure-utils

# or

yarn add sure-utils

# or

pnpm i sure-utils
```

## Usage
Expand Down
25 changes: 25 additions & 0 deletions has.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { hasOwnProperty } from './.internal/staticFucs'

/**
* Check the key is the val own property
*
* @param {object} val the check value
* @param {string | symbol} key the check key
* @returns boolean
*/
const hasOwnProp = (
val: object,
key: string | symbol
): key is keyof typeof val => hasOwnProperty.call(val, key)

/**
* Check ths value is changed or not
*
* @param value the new value
* @param oldValue the old value
* @returns boolean
*/
const hasChanged = (value: any, oldValue: any): boolean =>
!Object.is(value, oldValue)

export { hasOwnProp, hasChanged }
45 changes: 25 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
/* Array function */
export * from './array'
/* Array funcs */
import { removeArrayItem } from './array'

// judge the value type
import isUndefined from './isUndefined'
import isNull from './isNull'
import isSymbol from './isSymbol'
import isNumber from './isNumber'
import isString from './isString'
import isArray from './isArray'
import isPlainObject from './isPlainObject'
import isObject from './isObject'
import isDate from './isDate'
import isFunction from './isFunction'
import isMap from './isMap'
import isSet from './isSet'
import isPromise from './isPromise'
// is funcs
import {
isUndefined,
isNull,
isSymbol,
isNumber,
isString,
isArray,
isPlainObject,
isObject,
isDate,
isFunction,
isMap,
isSet,
isPromise
} from './is'

// has funcs
import hasOwnProp from './hasOwnProp'
import hasChanged from './hasChanged'
import { hasChanged, hasOwnProp } from './has'

// get values
import getPrototype from './getPrototype'
Expand All @@ -43,7 +44,8 @@ export {
hasOwnProp,
hasChanged,
getPrototype,
debounce
debounce,
removeArrayItem
}

export default {
Expand All @@ -63,5 +65,8 @@ export default {
hasOwnProp,
hasChanged,
getPrototype,
debounce

debounce,

removeArrayItem
}
160 changes: 160 additions & 0 deletions is.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { toTypeString } from './.internal/staticFucs'
import getPrototype from './getPrototype'
import {
arrayTag,
dateTag,
mapTag,
setTag,
objectTag
} from './.internal/constants'

/**
* Check if `value` is `null`
*
* @param val
* @returns
*/
const isNull = (val: unknown): val is null => val === null

/**
* Check if `value` is `undefined`
*
* @param val the check value
* @returns boolean
*/
const isUndefined = (val: unknown): val is undefined => val === undefined

/**
* Check if `value` is `string`
*
* @param val the check value
* @returns boolean
*/
const isString = (val: unknown): val is string => typeof val === 'string'

/**
* Check if `value` is `symbol`
*
* @param val the check value
* @returns boolean
*/
const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'

/**
* Check if `value` is like an `object`
*
* @param val the check value
* @returns boolean
*/
const isObject = (val: unknown): val is Record<any, any> => {
return typeof val === 'object' && !isNull(val)
}

/**
* Check if `value` is `array`
*
* @param val the check value
* @returns boolean
*/
const isArray = (val: unknown): val is Array<any> => {
if (Array.isArray) {
return Array.isArray(val)
}
// call `Object.prototype.toString` to check it
return isObject(val) && toTypeString(val) === arrayTag
}

/**
* Check if `value` is `Date`
*
* @param val the check value
* @returns boolean
*/
const isDate = (val: unknown): boolean => {
// call `Object.prototype.toString` to check
return isObject(val) && toTypeString(val) === dateTag
}

/**
* Check if `value` is like an `function`
*
* @param val the check value
* @returns boolean
*/
const isFunction = (val: unknown): val is Function => typeof val === 'function'

/**
* Check if `value` is `map`
*
* @param val the check value
* @returns boolean
*/
const isMap = (val: unknown): val is Map<any, any> => {
return isObject(val) && toTypeString(val) === mapTag
}

/**
* Check if `value` is `number`
*
* @param val the check value
* @returns boolean
*/
const isNumber = (val: unknown): val is number => typeof val === 'number'

/**
* Check if `value` is a `plain object`
*
* @param val the check value
* @returns boolean
*/
const isPlainObject = (val: unknown): val is object => {
if (!isObject(val) || toTypeString(val) !== objectTag) {
return false
}
if (getPrototype(val) === null) {
return true
}
let proto: object | null = val
// it may be some plain object chain
while (getPrototype(proto) !== null) {
proto = getPrototype(proto)
}
// proto is Object.prototype
return getPrototype(val) === proto
}

/**
* Check if `value` is `Promise`
*
* @param {unknown} val the check value
* @returns {boolean} boolean
*/
const isPromise = <T = any>(val: unknown): val is Promise<T> => {
return isObject(val) && isFunction(val.then) && isFunction(val.catch)
}

/**
* Check if `value` is `set`
*
* @param val the check value
* @returns boolean
*/
const isSet = (val: unknown): val is Set<any> => {
return isObject(val) && toTypeString(val) === setTag
}

export {
isNull,
isUndefined,
isString,
isSymbol,
isObject,
isArray,
isFunction,
isDate,
isMap,
isSet,
isNumber,
isPlainObject,
isPromise
}
19 changes: 0 additions & 19 deletions isArray.ts

This file was deleted.

16 changes: 0 additions & 16 deletions isDate.ts

This file was deleted.

10 changes: 0 additions & 10 deletions isFunction.ts

This file was deleted.

15 changes: 0 additions & 15 deletions isMap.ts

This file was deleted.

9 changes: 0 additions & 9 deletions isNull.ts

This file was deleted.

9 changes: 0 additions & 9 deletions isNumber.ts

This file was deleted.

13 changes: 0 additions & 13 deletions isObject.ts

This file was deleted.

28 changes: 0 additions & 28 deletions isPlainObject.ts

This file was deleted.

Loading

0 comments on commit e00f8cf

Please sign in to comment.