1.0.5 โ€ข Published 2 years ago

@oreo9875/web-app-utils v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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

APIDescribe
storage.setStorageSet up a time-sensitive localstorage
storage.getStorageGet a time-sensitive localstorage
storage.removeStorageRemove a time-sensitive localstorage
deepCopyDeep copy an object or an array
deepEqualDeeply compare whether two objects or arrays are the same
getTypeGet detailed data types
debounceDebounce a function
throttleThrottle a function
judgePlainObjectDetermine whether an object is an ordinary object
token.setTokenSet encryption token through json-web-token
token.getTokenDecrypt token

๐Ÿ”ง Usage

- storage

storage.setStorage

Params

  • key string: The key of localstorage that needs to be stored
  • value string: The value of localStorage that needs to be stored
  • aging 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 stored
  • return 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 copied
  • return 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 deeply
  • obj2 Array|Object: The object2 or array2 that need to be compared deeply
  • return boolean: Returns whether two values are deeply equal

Example

const result = _.deepEqual(obj1, obj2);

- getType

Params

  • value any: The data that needs to be typed
  • return 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-shake
  • delay number: The delay time that needs to be configured for the function, in milliseconds
  • return 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 throttle
  • interval number: Throttling Interval

  • return Function: The processed function

Example

const new_func = _.throttle(old_func, 1000);

- judgePlainObject

Params

  • obj Object: Object to determine whether it is a plain object
  • return 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 contain
  • secret string: The encryption key
  • age string: Token validity period
  • return 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 solved
  • secret string: The encryption key
  • return { 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#');
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago