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

kaka-localstorage

v1.0.2

Published

kaka-localstorage

Downloads

26

Readme

LocalStorageHelper

LocalStorageHelper 是一个轻量级的封装库,用于简化对浏览器 localStorage 的操作,支持设置过期时间、更新、删除、以及判断键值对的存在性。

安装

通过 npm 安装:

npm install kaka-localstorage

或通过 yarn 安装:

yarn add kaka-localstorage

使用方法

引入

在你的项目中引入 LocalStorageHelper

import LocalStorageHelper from 'kaka-localstorage'

方法

setItem(key, value, ttl = null)

设置一个键值对,同时可以设置过期时间(单位:秒)。

参数:

  • key (string): 存储的键名。
  • value (any): 需要存储的值。
  • ttl (number | null, 可选): 存储项的生存时间,单位为秒。如果不设置或为 null,则不会过期。

示例:

LocalStorageHelper.setItem('username', 'JohnDoe', 3600) // 设置 1 小时的过期时间
LocalStorageHelper.setItem('isLoggedIn', true) // 永不过期

getItem(key)

获取某个键值对,如果已过期,则返回 null

参数:

  • key (string): 要获取的键名。

返回:

  • value (any | null): 存储的值或 null(如果已过期或不存在)。

示例:

const username = LocalStorageHelper.getItem('username')

removeItem(key)

删除某个键值对。

参数:

  • key (string): 要删除的键名。

示例:

LocalStorageHelper.removeItem('username')

clearAll()

清空所有的 localStorage 项。

示例:

LocalStorageHelper.clearAll()

updateItem(key, newValue, ttl = null)

更新某个键值对的值和过期时间。

参数:

  • key (string): 要更新的键名。
  • newValue (any): 新的值。
  • ttl (number | null, 可选): 新的过期时间,单位为秒。

示例:

LocalStorageHelper.updateItem('username', 'JaneDoe', 7200) // 更新为 2 小时

hasItem(key)

判断某个键是否存在(且未过期)。

参数:

  • key (string): 要检查的键名。

返回:

  • boolean: true 如果键存在且未过期,否则为 false

示例:

const exists = LocalStorageHelper.hasItem('username')

许可证

本项目使用 MIT 许可证,详情请查看 LICENSE 文件。