Stay Hungry,Stay Foolish!

几个流行的npm包

cookie操作

浏览器原生的JS接口操作cookie非常难用。

于是产生了好用的js包。

https://www.npmjs.com/package/js-cookie

A simple, lightweight JavaScript API for handling cookies

 

基本用法

Basic Usage

Create a cookie, valid across the entire site:

Cookies.set('name''value');

Create a cookie that expires 7 days from now, valid across the entire site:

Cookies.set('name''value'{ expires});

Create an expiring cookie, valid to the path of the current page:

Cookies.set('name''value'{ expires7, path'});

Read cookie:

Cookies.get('name')// => 'value'
Cookies.get('nothing')// => undefined

Read all visible cookies:

Cookies.get()// => { name: 'value' }

 

 

客户端持久化方法

https://www.npmjs.com/package/store2

存储数据到浏览器本地,包括 本地存储 和 会话存储。

A feature-filled and friendly way to take advantage of localStorage and sessionStorage (JSON, namespacing, extensions, etc).

Download: store2.min.js or store2.js
NPM: npm install store2
NuGet: Install-Package store2

基本用法

The main store function can handle set, get, transact, setAll, getAll, each, and clear actions directly. Respectively, these are called like so:

store(key, data);                 // sets stringified data under key
store(key);                       // gets and parses data stored under key
store(key, fn[, alt]);            // run transaction function on/with data stored under key
store({key: data, key2: data2});  // sets all key/data pairs in the object
store();                          // gets all stored key/data pairs as an object
store((keydata)=>});          // calls function for each key/data in storage, return false to exit
store(false);                     // clears all items from storage

Parameters in [brackets] are optional. There are also more explicit and versatile functions available:

store.set(key, data[, overwrite])// === store(key, data);
store.setAll(data[, overwrite]);   // === store({key: data, key2: data});
store.get(key[, alt]);             // === store(key);
store.getAll([fillObj]);           // === store();
store.transact(key, fn[, alt]);    // === store(key, fn[, alt]);
store.clear();                     // === store(false);
store.has(key);                    // returns true or false
store.remove(key[, alt]);          // removes key and its data, then returns the data or alt, if none
store.each(fn[, fill]);            // === store(fn); optional call arg will be 3rd fn arg (e.g. for gathering values)
store.add(key, data);              // concats, merges, or adds new value into existing one
store.keys([fillList]);            // returns array of keys
store.size();                      // number of keys, not length of data
store.clearAll();    

 

无服务mock测试

不需要架设服务器,不同于json-server。

简化模拟测试方法。

原理是,通过js方法,接管ajax服务器请求方法,直接从本地json数据中获取。

Mock.js is a simulation data generator to help the front-end to develop and prototype separate from the back-end progress and reduce some monotony particularly while writing automated tests.

The official site: http://mockjs.com

Features

  • Generate simulated data according to the data template
  • Provide request/response mocking for ajax requests
  • Generate simulated data according to HTML-based templates

This library is loosely inspired by Elijah Manor's post Mocking Introduction, mennovanslooten/mockJSON, appendto/jquery-mockjax and victorquinn/chancejs.

 

posted @ 2020-03-26 22:43  lightsong  阅读(1717)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel