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

noshjs

v0.4.0

Published

一个我自己常用的工具函数库。

Downloads

31

Readme

nosh.js Build Status Coverage Status dependencies Status NPM Version

一个我自己常用的工具函数库。

安装

可以用 NPM 或者 Yarn 安装:

npm i noshjs

或者直接将 nosh.js 下载到你的项目中,通过 script 标签引用,此时会注册一个全局变量 window.nosh

API

nosh.copy(value)

类似于 lodash.cloneDeep,但没有对特殊类型(例如 Set、Map、Buffer 等)做处理,所以性能比 lodash.cloneDeep 高,见 https://github.com/lodash/lodash/issues/1984

nosh.extend(object, [sources])

类似于 lodash.merge,但没有对特殊类型做处理。

nosh.get(object, path, [defaultValue])

类似于 lodash.get,但是在读取到 null 时也会返回 defaultValue。例如:

lodash.get({ a: null }, 'a', 'default value') // null
nosh.get({ a: null }, 'a', 'default value') //  'default value'

这个方法性能比 lodash.get 低,如果介意的话,可以基于 lodash.get 封装:

function noshGet(object, path, defaultValue) {
  let result = lodash.get(object, path, defaultValue)
  if (result === null) result = defaultValue
  return result
}

nosh.isNumber(value)

判断 value 是否满足 typeof value === 'number' && !isNaN(value)

nosh.kmbt(value, [fixed = 2])

将一个数字转换成 KMBT 表现形式的字符串。如果 value 不能转换成数字,则返回 null。第二个参数可以设置保留多少位小数,默认保留两位。

nosh.kmbt('not a number') // null
nosh.kmbt(988) // '988'
nosh.kmbt(9888) // '9.89K'
nosh.kmbt(9888777) // '9.89M'
nosh.kmbt(98887776666) // '98.89B'
nosh.kmbt(9888777666555) // '9.89T'
nosh.kmbt(9888777666555444) // '9.89P'
nosh.kmbt(9888777666555444333) // '9.89E')

nosh.obj2qs(object, [prefix])

将一个对象转换成查询字符串。第二个参数用于指定查询字符串的前缀。

nosh.obj2qs({ a: 1, b: 2 }, '?') // '?a=1&b=2'

nosh.percentage(value, [fixed = 2])

将一个数字转换成百分比表现形式的字符串。如果 value 不能转换成数字,则返回 null。第二个参数可以设置保留多少位小数,默认保留两位。

nosh.percentage('not a number') // null
nosh.percentage('1') // '100%'
nosh.percentage(-0.1) // '-10%'

nosh.remove(array, item)

从数组中删除指定的元素,这个方法只删除第一个匹配的元素。

nosh.remove([1, 2, 3, 1], 1) // [2, 3, 1]

nosh.thousands(value)

将一个数字转换成百分比表现形式的字符串。如果 value 不能转换成数字,则返回 null

nosh.thousands('not a number') // null
nosh.thousands(1000) // '1,000'

许可

MIT