@oreo9875/web-app-utils
v1.0.5
Published
This toolkit is used to assist web application developers to complete some common but tedious coding tasks.
Downloads
1
Readme
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
key
string: The key of localstorage that needs to be storedvalue
string: The value of localStorage that needs to be storedaging
number: The effective duration of storage, in hours (optional, by default the time limit is permanent)return
boolean: 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
key
string: The key of localstorage that needs to be storedreturn
string|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
key
string: The key of localstorage that needs to be removed
Example
_.storage.removeStorage('a');
- deepCopy
Params
obj
Array|Object: The object or array that needs to be deep copiedreturn
Array|Object: The result of a deep copy
Example
const obj_clone = _.deepCopy(obj1);
- deepEqual
Params
obj1
Array|Object: The object1 or array1 that need to be compared deeplyobj2
Array|Object: The object2 or array2 that need to be compared deeplyreturn
boolean: Returns whether two values are deeply equal
Example
const result = _.deepEqual(obj1, obj2);
- getType
Params
value
any: The data that needs to be typedreturn
string: 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
func
Function: The function that needs anti-shakedelay
number: The delay time that needs to be configured for the function, in millisecondsreturn
Function: 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
func
Function: The function that needs throttleinterval
number: Throttling Intervalreturn
Function: The processed function
Example
const new_func = _.throttle(old_func, 1000);
- judgePlainObject
Params
obj
Object: Object to determine whether it is a plain objectreturn
boolean: Returns true if the input object is a plain object, otherwise returns false
Example
const isPlainObject = _.judgePlainObject(obj);
- token
token.setToken
Params
data
Object: The data that the token needs to containsecret
string: The encryption keyage
string: Token validity periodreturn
string: A JSON Web Token string
Example
const token = _.token.setToken({name: 'xxx', phone: 'xxx'}, 'husib&hs2#', '2d');
token.getToken
Params
token
string: The token that needs to be solvedsecret
string: 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#');