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

mp-cookie

v1.0.1

Published

An implementation of simulated cookie operation for wechat miniprogram

Downloads

1

Readme

mp-cookie

使用微信小程序storage模拟cookie库的操作,支持设置设置过期时间,支持获取过期后的cookie。

Usage

初始化npm

参考:npm支持

安装依赖:

npm i mp-cookie --save
# or
yarn add mp-cookie

构建npm

参考:npm支持

使用mp-cookie

// pages/index/index.js
import MpCookie from 'mp-cookie'
// 创建实例
const cookie = new MpCookie();
Page({
  data: {
    userInfo: {},
  },
  onShow() {
    const userInfo = cookie.get('USER_INFO')
    this.setData({
      userInfo,
    })
  }
})

API

配置

options.path

cookie生效路径,默认为空, 即全局生效,如果希望设置为当前页面生效,可以参考如下代码:

const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
const cookie = new MpCookie({ path: currentPage.route })
options.expires

cookie过期时间,默认1天。这里之所以默认配置为短期失效,是为了更贴近原始cookie的特性。如果希望设置永久cookie,最好这里指定一个很大的数字

// 失效日期10年
const cookie = new MpCookie({ expires: 10 * 365 * 24 * 60 * 60 * 1000 })

方法

cookie.get(KEY)

获取键为KEY的cookie。

cookie.set(KEY, VALUE)

种入cookie,键为KEY, 值为VALUE, VALUE可以是任意可JSON.stringify的数据。

cookie.remove(KEY)

移除键为KEY的cookie。

cookie.clear()

清空当前实例的所有cookie。

cookie.getExpired(KEY)

获取已过期的cookie。为方便获取已过期数据,单独提供了这个方法。

实践

通常情况,一个小程序中往往只使用一个cookie实例。建议在公共文件管理cookie,调用方只需引入全局唯一cookie实例。

//cookieManage.js
import MpCookie from 'mp-cookie'
export default new MpCookie({ expires: 10 * 365 * 24 * 60 * 60 * 1000 })

// pages/page/index.js
import cookie from '../cookieManage.js'
Page({
  onShow() {
    const userInfo = cookie.get('USER_INFO')
    this.setData({
      userInfo,  
    })  
  } 
})

TODO

  • []storage存储大小管理
  • []支持长效cookie的简便写法