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 @   lightsong  阅读(1729)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示