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

global-var-manager

v1.0.0

Published

一个js全局变量管理器

Downloads

2

Readme

GlobalVar

一个js全局变量管理器

插件的设计初衷

我们经常需要在项目中使用到很多的全局变量例如:token令牌,但是全局变量可能是由不同的执行时间产生的这样就使得全局变量的产生和使用散乱在各个代码中而难于管理。

插件的设计思想

  1. 统一项目中全局变量的存放、获取和删除。

构建配置抽离成npm包的意义

通用性
  1. 业务开发者无需关注构建配置
  2. 统一团队构建脚本
可维护性
  1. 构建配置合理的拆分
  2. README 文档、ChangeLog文档等
质量
  1. 冒烟测试、单元测试、测试覆盖率
  2. 持续集成

编辑器

  1. Visual Studio Code

语言

  1. javascript ES6

构建工具

  1. "webpack": "^4.41.2"
  2. "webpack-cli": "^3.3.9"

构建命令

  1. npm run build

更新状态

版本 | 时间 ---|--- 1.0.0 | 2019-11-07


库目录结构

未压缩版: global-var-manager.js
压缩版:global-var-manager.min.js

这样打包文件的体积将大幅缩小。


使用

使用npm

$ npm install global-var-manager --save

使用cdn

<script type="text/javascript" src="global-var-manager.min.js"></script>

示例:

// 导入插件
import GlobalVarManagerLibrary from 'global-var-manager'

// 初始化插件并传入需要在初始化时立马构建的全局变量对象
let globalVar = new GlobalVarManagerLibrary ({'token': 'test_123'})
// 获取value,如果没有token就用test
globalVar.getValue('token', 'test')


类: GlobalVar

构造器 Constructor

new GlobalVar(globalVarObject)

构造函数接收1个参数,第一个参数为可选

参数:

参数 | 类型 | 属性 | 默认值 | 描述 ---|---|---|---|--- globalVarObject | Object | 可选 | {} | 初始化时立马构建的全局变量

示例

new GlobalVar({'token': 'test_123'})

操作函数

函数:getValue(key, defaultValueopt)

说明:获取某个全局变量的value,没有获取到将返回null

名称 | 类型 | 属性 | 默认值 | 描述 ---|---|---|---|--- key | string | | | 获取全局变量的key defaultValueopt | * | 可选 | | 如果key没有对应的value那么返回传入的默认值

实例:

let globalVar = new GlobalVar({'token': 'test_123'})

globalVar.getValue('token', 'test')

函数:addOrUpdate(key, value)

说明:添加或者更新某个key的value

名称 | 类型 | 默认值 | 描述 ---|---|---|--- key | string | | key键 value | * | | value值

实例:

let GlobalVar = new GlobalVar({'token': 'test_123'})
GlobalVar.addOrUpdate('token', 'test')

函数:evict(key)

说明:删除某个key对应的全局变量

名称 | 类型 | 默认值 | 描述 ---|---|---|--- key | string | | | key键

实例:

let GlobalVar = new GlobalVar({'token': 'test_123'})
GlobalVar.evict('token')

函数:evictAll()

说明:清空所有全局变量

实例:

let GlobalVar = new GlobalVar({'token': 'test_123'})
GlobalVar.evictAll()