库 | 做用 |
---|---|
chalk-pipe | 使用更简单的样式字符串建立粉笔样式方案 |
chalk | 正确处理终端字符串样式 |
Commander.js | 完整的 node.js 命令行解决方案 |
Inquirer.js | 一组通用的交互式命令行用户界面。 |
slash | 系统路径符处理 |
minimist | 解析参数选项 |
dotenv | 将环境变量从 .env文件加载到process.env中 |
dotenv-expand | 扩展计算机上已经存在的环境变量 |
hash-sum | 很是快的惟一哈希生成器 |
deepmerge | 深度合并两个或多个对象的可枚举属性。 |
yaml-front-matter | 解析yaml或json |
resolve | 实现node的 require.resolve() 算法,这样就能够异步和同步地使用require.resolve()表明文件 |
semver | npm的语义版本器 |
leven | 测量两字符串之间的差别<br/>最快的JS实现之一 |
lru cache | 删除最近最少使用的项的缓存对象 |
portfinder | 自动寻找 8000 至65535 内可用端口号 |
ora | 优雅的终端转轮 |
envinfo | 生成故障排除软件问题(如操做系统、二进制版本、浏览器、已安装语言等)时所需的通用详细信息的报告 |
memfs | 内存文件系统与Node's fs API相同实现 |
execa | 针对人类的流程执行 |
webpack-merge | 用于链接数组和合并对象,从而建立一个新对象 |
webpack-chain | 使用链式API去生成简化webpack版本配置的修改 |
strip-ansi | 从字符串中去掉ANSI转义码 |
address | 获取当前机器的IP, MAC和DNS服务器。 |
default-gateway | 经过对OS路由接口的exec调用得到机器的默认网关 |
joi | JavaScript最强大的模式描述语言和数据验证器。 |
fs-extra | 添加了未包含在原生fs 模块中的文件系统方法,并向fs 方法添加了promise支持 |
Acorn | 一个小而快速的JavaScript解析器,彻底用JavaScript编写。 |
zlib.js | ZLIB.js是ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952)和PKZIP在JavaScript实现。 |
nodejs交互工具库 -- chalk-pipe和chalk前端
nodejs交互工具库 -- commander和Inquirernode
nodejs交互工具库 -- slash, minimist和dotenv, dotenv-expandwebpack
nodejs交互工具库 -- hash-sum, deepmerge和yaml-front-mattergit
nodejs交互工具库 -- resolve和semvergithub
nodejs交互工具库 -- leven, lru cache和portfinderweb
nodejs交互工具库 -- webpack-merge和webpack-chainjson
nodejs交互工具库 -- strip-ansi, address, default-gateway和joisegmentfault
nodejs交互工具库 -- fs-extra, Acorn和zlib
很是快的惟一哈希生成器
yarn add hash-sum
const sum = require('hash-sum'); console.log(sum([ 0, 1, 2, 3 ])) // 00a34759 console.log(sum('1988-06-09T03:00:00.000Z')) // dff5ee3c
sum(value)
生成基于的4字节16进制哈希 value
.
# creates unique hashes 00a34759 from: [ 0, 1, 2, 3 ] a8996f0c from: { '0': 0, '1': 1, '2': 2, '3': 3 } 5b4c2116 from: { '0': 0, '1': 1, '2': 2, '3': 3, length: 4 } 2c937c45 from: { url: 12 } 31d55010 from: { headers: 12 } 2d2e11bc from: { headers: 122 } ec99d958 from: { headers: '122' } 18c00eee from: { headers: { accept: 'text/plain' } } 6cb332c8 from: { payload: [ 0, 1, 2, 3 ], headers: [ { a: 'b' } ] } 12ff55db from: { a: [Function: a] } 46f806d2 from: { b: [Function: b] } 0660d9c4 from: { b: [Function: b] } 6c95fc65 from: function () {} 2941766e from: function (a) {} 294f8def from: function (b) {} 2d9c0cb8 from: function (a) { return a;} ed5c63fc from: function (a) {return a;} bba68bf6 from: '' 2d27667d from: 'null' 774b96ed from: 'false' 2d2a1684 from: 'true' 8daa1a0c from: '0' 8daa1a0a from: '1' e38f07cc from: 'void 0' 6037ea1a from: 'undefined' 9b7df12e from: null 3c206f76 from: false 01e34ba8 from: true 8a8f9624 from: Infinity 0315bf8f from: -Infinity 64a48b16 from: NaN 1a96284a from: 0 1a96284b from: 1 29172c1a from: undefined 59322f29 from: {} 095b3a22 from: { a: {}, b: {} } 63be56dd from: { valueOf: [Function: valueOf] } 63be4f5c from: { valueOf: [Function: valueOf] } 5d844489 from: [] ba0bfa14 from: 2019-06-28T21:24:31.215Z 49324d16 from: 2019-06-28T03:00:00.000Z 434c9188 from: 1988-06-09T03:00:00.000Z ce1b5e44 from: global
基本经常使用的方法场景就这些了,更完整的用法能够直接查阅文档
深度合并两个或多个对象的可枚举属性。
UMD包是723B minified+gzipped
const x = { foo: { bar: 3 }, array: [{ does: 'work', too: [1, 2, 3] }] } const y = { foo: { baz: 4 }, quux: 5, array: [{ does: 'work', too: [4, 5, 6] }, { really: 'yes' }] } const output = { foo: { bar: 3, baz: 4 }, array: [{ does: 'work', too: [1, 2, 3] }, { does: 'work', too: [4, 5, 6] }, { really: 'yes' }], quux: 5 }
在 npm 这么作:
npm install deepmerge
deepmerge能够直接在浏览器中使用,而不须要使用包管理器/绑定器: UMD version from unpkg.com.
deepmerge暴露了一个CommonJS入口点:
const merge = require('deepmerge')
ESM入口点被丢弃由于 Webpack bug.
merge(x, y, [options])
深度合并两个对象x和y,返回一个新的合并对象,其中包含来自x和y的元素。
若是x和y有一个键相同的元素,那么y的值将出如今结果中。
合并建立一个新对象,所以x或y都不会被修改。
注意: 默认状况下,数组是经过链接合并的。
merge.all(arrayOfObjects, [options])
将任意数量的对象合并为单个结果对象。
const foobar = { foo: { bar: 3 } } const foobaz = { foo: { baz: 4 } } const bar = { bar: 'yay!' } merge.all([ foobar, foobaz, bar ]) // => { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
arrayMerge
有多种方法合并两个数组,下面是一些示例,但您也能够建立本身的自定义函数。
你的 arrayMerge
函数将会被调用,有三个参数:目标数组,源数组和选项对象。
isMergeableObject(value)
cloneUnlessOtherwiseSpecified(value, options)
arrayMerge
示例:覆盖目标数组彻底覆盖现有数组值,而不是链接它们:
const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray merge( [1, 2, 3], [3, 2, 1], { arrayMerge: overwriteMerge } ) // => [3, 2, 1]
arrayMerge
example: combine arrays组合两个数组中相同索引处的对象。
这是默认的数组合并算法 pre-version-2.0.0.
const combineMerge = (target, source, options) => { const destination = target.slice() source.forEach((item, index) => { if (typeof destination[index] === 'undefined') { destination[index] = options.cloneUnlessOtherwiseSpecified(item, options) } else if (options.isMergeableObject(item)) { destination[index] = merge(target[index], item, options) } else if (target.indexOf(item) === -1) { destination.push(item) } }) return destination } merge( [{ a: true }], [{ b: true }, 'ah yup'], { arrayMerge: combineMerge } ) // => [{ a: true, b: true }, 'ah yup']
isMergeableObject
默认状况下,deepmerge克隆几乎全部类型对象的全部属性。
若是您的对象是特殊类型的,而且您但愿复制整个对象而不单单是复制其属性,那么您可能不但愿这样作。
您能够经过为 isMergeableObject
选项传递一个函数来实现这一点。
若是您只想克隆普通对象的属性,而忽略全部“特殊”类型的实例化对象,那么您可能须要加入 is-plain-object
.
const isPlainObject = require('is-plain-object') function SuperSpecial() { this.special = 'oh yeah man totally' } const instantiatedSpecialObject = new SuperSpecial() const target = { someProperty: { cool: 'oh for sure' } } const source = { someProperty: instantiatedSpecialObject } const defaultOutput = merge(target, source) defaultOutput.someProperty.cool // => 'oh for sure' defaultOutput.someProperty.special // => 'oh yeah man totally' defaultOutput.someProperty instanceof SuperSpecial // => false const customMergeOutput = merge(target, source, { isMergeableObject: isPlainObject }) customMergeOutput.someProperty.cool // => undefined customMergeOutput.someProperty.special // => 'oh yeah man totally' customMergeOutput.someProperty instanceof SuperSpecial // => true
customMerge
指定一个函数,可用于根据属性名称重写属性的默认合并行为。
customMerge
函数将传递每一个属性的键值,并将返回用于合并该属性值的函数。
它也可能返回 undefined
,在这种状况下,将使用默认的合并行为。
const alex = { name: { first: 'Alex', last: 'Alexson' }, pets: ['Cat', 'Parrot'] } const tony = { name: { first: 'Tony', last: 'Tonison' }, pets: ['Dog'] } const mergeNames = (nameA, nameB) => `${nameA.first} and ${nameB.first}` const options = { customMerge: (key) => { if (key === 'name') { return mergeNames } } } const result = merge(alex, tony, options) result.name // => 'Alex and Tony' result.pets // => ['Cat', 'Parrot', 'Dog']
clone
Deprecated.
Defaults to true
.
若是clone
是 false
而后将直接复制子对象,而不是克隆。这是版本2.x以前的默认行为。
基本经常使用的方法场景就这些了,更完整的用法能够直接查阅文档
在字符串的前面解析yaml或json。将解析后的内容和字符串内容的其他部分放入对象文字中。
这个自述文件是针对4.x版本,它引入了破坏性的更改。查看 changelog 取得进一步资讯
This
--- name: Derek Worthen age: 127 contact: email: email@domain.com address: some location pets: - cat - dog - bat match: !!js/regexp /pattern/gim run: !!js/function function() { } --- Some Other content var fs = require('fs'); var yamlFront = require('yaml-front-matter'); fs.readFile('./some/file.txt', 'utf8', function(fileContents) { console.log(yamlFront.loadFront(fileContents)); });
outputs
{ name: 'Derek Worthen', age: 127, contact: { email: 'email@domain.com', address: 'some location' }, pets: [ 'cat', 'dog', 'bat' ], match: /pattern/gim, run: [Function], __content: '\nSome Other Content' }
May also use JSON
--- { "name": "Derek Worthen", "age": "young", "anArray": ["one","two"], "subObj":{"field1": "one"} } --- Some content
NOTE:---
须要表示前端内容的开始和结束。在开始后必须有一个换行---
和结束前的换行符---
.
$ npm install yaml-front-matter
若是您计划使用命令行工具,请使用-g标志。
$ npm install yaml-front-matter -g
var yamlFront = require('yaml-front-matter');
dist/yamlFront.js 客户端脚本将把yaml-front-matter库公开为全局, yamlFront
. 客户端脚本 js-yaml 也是必需的。在某些用例中可能须要加载espirma。看 js-yaml 取得进一步资讯
<script src="https://unpkg.com/js-yaml@3.10.0/dist/js-yaml.js"></script> <script src="yamlFront.js"></script> <script> // parse front matter with yamlFront.loadFront(String); </script>
Note: yaml-front-matter 是做为umd包交付的,因此它应该在commonjs, amd和浏览器(做为一个全局)环境中工做。
$ npm install --dev && npm start
而后参观 localhost:8080
.
将构建文件输出到 dist/
.
$ npm install --dev && npm run build
npm install --dev && npm test
Usage: yaml-front-matter [options] <yaml-front-matter content> Options: -h, --help output usage information -v, --version output the version number -c, --content [name] set the property name for the files contents [__content] --pretty formats json output with spaces.
注意,cli使用
safeLoadFront
,所以不会解析包含regexp、函数或未定义值的yaml。
# Piping content from one file, through yaml parser and into another file cat ./some/file.txt | yaml-front-matter > output.txt
Yaml前端内容包装了js-yaml 以支持解析Yaml前端内容。
var input = [ '---\npost: title one\n', 'anArray:\n - one\n - two\n', 'subObject:\n prop1: cool\n prop2: two', '\nreg: !!js/regexp /pattern/gim', '\nfun: !!js/function function() { }\n---\n', 'content\nmore' ].join(''); var results = yamlFront.loadFront(input); console.log(results);
outputs
{ post: 'title one', anArray: [ 'one', 'two' ], subObject: { obj1: 'cool', obj2: 'two' }, reg: /pattern/gim, fun: [Function], __content: '\ncontent\nmore' }
Front-matter is optional.
yamlFront.loadFront('Hello World'); // => { __content: "Hello World!" }
Content is optional
yamlFront.loadFront(''); // => { __content: '' }
api与loadFront相同,只是它不支持regexps、函数或undefined。有关更多信息,请参阅js-yaml 。
options对象支持与 js-yaml 相同的选项,并添加了对附加键的支持。
options.contentKeyName
: 指定用于存储未被yaml-front-matter解析的内容的对象键。默认为 __content
.yamlFront.loadFront('Hello World', { contentKeyName: 'fileContents' }); // => { fileContents: "Hello World" }
基本经常使用的方法场景就这些了,更完整的用法能够直接查阅文档