npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

n-localstorage

v2.0.0

Published

Encapsulate a simple LocalStorage Class

Downloads

10

Readme

LocalStorage

NPM version NPM travis codebeat badge codecov

Introduction

让使用 localStorage 变得更简单

  1. 原生 localStorage 只能存储字符串
  2. 对于复杂数据结构,在存储之前,必须先进行序列化
  3. 对于复杂数据结构,在读取的时候,又需要反序列化
  4. 直接操作数据,可能会对数据造成覆盖,中间最好有一层检测机制,保证数据的准确性
  5. 对于 查询当前剩余容量 获取key列表 等一些快捷操作,也做了封装

针对上述,对 localStorage 进行一次封装,默认对数据进行序列化、反序列化、覆盖提醒、快捷处理等操作,用户只需关心自己要保存、获取的数据的一系列操作。

Usage

ES6

import LS from 'n-localstorage';

UMD

// download files
use n-localstorage/dist/localstorage.js

set(key, value, cover)

存储数据,返回 set 的对象
* set(key, value, cover = false)
* @params key
* @params value
* @params cover 是否覆盖
* return {
    key: key,
    val: value
}

Example:

LS.set('name', 'realign');
/**
return
{
    key: 'name',
    val: 'realign'
}
*/

get(key)

获取数据,返回 对应值(会保持原来的数据类型)
* get(key)
* return valye

Example:

LS.get('name'); // 'realign'

has(key)

判断是否存在此 key,返回 boolean
* has(key)
* return boolean

Example:

LS.has('name'); // true
LS.has('my'); // false

remove(key)

删除 key 对应的这条数据,
返回 boolean,该条数据是否仍存在
    已成功删除 true(传入不存在的key,仍会返回 true)
    未成功删除 false
* remove(key)
* return boolean

Example:

LS.remove('name'); // true

clear

清空当前域下 localstorage 数据
* clear()
* return boolean

Example:

LS.clear(); // true

getKeyList

获取 localstorage 所有 key
* getKeyList()
* return array 默认 []

Example:

LS.getKeyList(); // ['name', 'age']

getAll

获取 localstorage 所有 数据
* getAll()
* return object 默认 {}

Example:

LS.getAll(); // {'name': 'realign', 'age': 23}

getSurplusCapacityKb

保留,但会有误差,建议用下面的新 api { getSurplusCapacity }

获取 localstorage 剩余容量(kb)
* getSurplusCapacityKb()
* return number

Example:

LS.getSurplusCapacityKb(); // 5119.998046875

getSurplusCapacity

/** 获取 localstorage 剩余容量
 * getSurplusCapacity(unit)
 * @param   unit => b | kb | mb
 * @returns number
 */

// Example:

LS.getSurplusCapacity('kb'); // 4119.998046875