1.0.5 โข Published 2 years ago
@oreo9875/web-app-utils v1.0.5
web-app-utils
This toolkit is used to assist web application developers to complete some common but tedious coding tasks.
import _ from 'web-app-utils';๐ Contents
๐งพ API List
| API | Describe |
|---|---|
| storage.setStorage | Set up a time-sensitive localstorage |
| storage.getStorage | Get a time-sensitive localstorage |
| storage.removeStorage | Remove a time-sensitive localstorage |
| deepCopy | Deep copy an object or an array |
| deepEqual | Deeply compare whether two objects or arrays are the same |
| getType | Get detailed data types |
| debounce | Debounce a function |
| throttle | Throttle a function |
| judgePlainObject | Determine whether an object is an ordinary object |
| token.setToken | Set encryption token through json-web-token |
| token.getToken | Decrypt token |
๐ง Usage
- storage
storage.setStorage
Params
keystring: The key of localstorage that needs to be storedvaluestring: The value of localStorage that needs to be storedagingnumber: The effective duration of storage, in hours (optional, by default the time limit is permanent)returnboolean: The operation returns 'true' on success and 'false' on failure
Example
_.storage.setStorage('a', 'b-value', 20);Store a localstorage whose key is 'a', value is 'b-value', and is valid for 20 hours.
storage.getStorage
Params
keystring: The key of localstorage that needs to be storedreturnstring|null: If the storage corresponding to 'key' is found and within the valid period, return the corresponding 'value', if not within the valid period or an error occurs, return 'null'
Example
_.storage.getStorage('a');storage.removeStorage
Params
keystring: The key of localstorage that needs to be removed
Example
_.storage.removeStorage('a');- deepCopy
Params
objArray|Object: The object or array that needs to be deep copiedreturnArray|Object: The result of a deep copy
Example
const obj_clone = _.deepCopy(obj1);- deepEqual
Params
obj1Array|Object: The object1 or array1 that need to be compared deeplyobj2Array|Object: The object2 or array2 that need to be compared deeplyreturnboolean: Returns whether two values are deeply equal
Example
const result = _.deepEqual(obj1, obj2);- getType
Params
valueany: The data that needs to be typedreturnstring: Data type
Example
const type = _.getType(value);- debounce
Returns a function that can be called multiple times (possibly in quick succession), but only fires the callback after waiting x milliseconds after the last call
Params
funcFunction: The function that needs anti-shakedelaynumber: The delay time that needs to be configured for the function, in millisecondsreturnFunction: The processed function
Example
const new_func = _.debounce(old_func, 1000);- throttle
Returns a function that can be called multiple times (possibly in quick succession), but only every x milliseconds, triggering the callback.
Params
funcFunction: The function that needs throttleintervalnumber: Throttling IntervalreturnFunction: The processed function
Example
const new_func = _.throttle(old_func, 1000);- judgePlainObject
Params
objObject: Object to determine whether it is a plain objectreturnboolean: Returns true if the input object is a plain object, otherwise returns false
Example
const isPlainObject = _.judgePlainObject(obj);- token
token.setToken
Params
dataObject: The data that the token needs to containsecretstring: The encryption keyagestring: Token validity periodreturnstring: A JSON Web Token string
Example
const token = _.token.setToken({name: 'xxx', phone: 'xxx'}, 'husib&hs2#', '2d');token.getToken
Params
tokenstring: The token that needs to be solvedsecretstring: The encryption keyreturn{ status: boolean, data: Object }: Decrypted data, when the status is true, the decryption is successful, and when it is false, the decryption fails
Example
const data = _.token.getToken('xxx', 'husib&hs2#');