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

mini-cookie

v0.0.6

Published

一个 Document.cookie 管理包 - A Document.cookie Manager Lib

Downloads

70

Readme

mini-cookie

mini-cookie 是一个轻量级的 JavaScript 库,旨在简化对浏览器 Document.cookie 的操作。它提供了一组简单易用的 API,允许开发者轻松地读取、设置和删除 cookie,而无需直接处理复杂的字符串操作。

  • 😄 无依赖包 - No Dependency
  • 🤡 支持广泛浏览器 - Support Extensive Browser
  • ✅ 支持 ESModule - Support ESM
  • ✅ 支持 CommonJS - Support CJS
  • ✅ 支持 UMD - Support UMD
  • ✅ 本身内置 TS 类型提示, 无需下载额外 @types 类型提示包 TS - Support TS
  • 👉 可自定义 Cookie TS 类型提示(重写模块 ICookieData 类型定义即可) - Support Custom TS

安装方式 - Installation

# pnpm
pnpm i mini-cookie

# yarn
yarn add mini-cookie

# npm
npm i mini-cookie

# bun
bun install mini-cookie

使用方式 - Usage

// ESM xxx.js
import MiniCookie from 'mini-cookie';
MiniCookie.set('cookieName', 'cookieValue')
console.log(MiniCookie.get('cookieName'))

// CJS xxx.js
const MiniCookie = require('mini-cookie');
MiniCookie.set('cookieName', 'cookieValue')
console.log(MiniCookie.get('cookieName'))

// UMD xxx.html 普通 script 直接导入 -> 访问全局变量 MiniCookie
<script src="https://unpkg.com/[email protected]"></script>;
<script>
  console.log("MiniCookie 包对象: ", MiniCookie)
  MiniCookie.set('cookieName', 'cookieValue')
  console.log(MiniCookie.get('cookieName'))
  console.log(MiniCookie.has('cookieName'))
</script>

// script type module 模块化内部引入方式
<script type="module">
  import MiniCookie from "https://unpkg.com/[email protected]/dist/index.esm.js";
  console.log("MiniCookie 包对象: ", MiniCookie)
  MiniCookie.set('cookieName', 'cookieValue')
  console.log(MiniCookie.get('cookieName'))
  console.log(MiniCookie.has('cookieName'))
  console.log(MiniCookie.del('cookieName'))
  console.log(MiniCookie.has('cookieName'))
</script>

miniCookie 方法 - API

| 方法名 | 描述 | 参数 | 返回值 | | --------- | ------------------------------------ | -------------------------------------------------- | ----------------- | | get | 获取 Cookie | get(key:string) | any | | set | 设置 Cookie | set(key:string, value:any,opts:IMiniCookieOpts) | boolean | | has | 判断 Cookie 是否存在 | has(key:string) | boolean | | del | 删除 Cookie | del(key:string) | boolean | | parse | 解析 Cookie 字符串 IMiniCookieObject | parse(cookie:string) | IMiniCookieObject | | serialize | 序列化 Cookie 对象 | serialize(key:string,val:any,opts:IMiniCookieOpts) | string |

IMiniCookieOpts 类型参数

| 属性 | 类型 | 描述 | 默认值 | | --- | --- | --- | --- | | domain | string | 域名 | 默认当前文档路径域部分 | | path | string | 路径 | 默认当前文档位置的路径 | | expires | Date | 过期时间 | 未设置则会在对话结束时过期 | | maxAge | number | 最大存活时间,单位(s), >MaxAge 优先级高于 Expires。(推荐) | 空 | | httpOnly | boolean | 是否阻止客户端脚本(如JavaScript)访问该Cookie, 只能在服务器端设置,不能在客户端设置。 | false | | secure | boolean | 是否只允许 HTTPS 请求访问 | false | | sameSite | "Strict", "Lax", "None" | 允许的跨域请求。Strict - 只允许同源的请求访问。 Lax - 允许跨域的请求访问。 None - 会在所有请求中发送,但需要同时设置Secure属性 | 空 | | partitioned | boolean | 是否开启分区。 | false | | priority | "High", "Medium", "Low" | 浏览器保留优先级权重。 High - 高保留权重 Medium - 中等保留权重 Low - 低保留权重 当Cookie达存储上限时低保留权重会被优先清除 | "Medium" |

覆写 ICookieData 获得自定义 TS 类型提示(可选)

// 覆写 ICookieData 类型接口已获得类型提示
// 例如: 在 type/xxx.d.ts | global.d.ts 中定义 ICookieData 类型接口
declare namespace MiniCookie {
  interface ICookieData {
    name:string
    age:number
  }
}
// 将 types/xxx.d.ts 加入到 tsconfig.json includes 中即可获得自定义类型提示功能咯

Blessing

🥰 献给所有追求简洁与规范代码的开发者,愿我们的代码如诗般优雅,逻辑清晰,易于维护。

License

MIT