npm收录了哪些包_几个流行的npm包

cookie操作

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

于是产生了好用的js包。

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

A simple, lightweight JavaScript API for handling cookies

Works in all browsers

Accepts any character

No dependency

Unobtrusive JSON support

Supports AMD/CommonJS

RFC 6265 compliant

Useful Wiki

~900 bytes gzipped!

基本用法

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: 7 });

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

Cookies.set('name', 'value', { expires: 7, 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((key, data)=>{ });          // 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值