nodejs交互工具库 -- hash-sum, deepmerge和yaml-front-matter

nodejs交互工具库系列

作用
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 测量两字符串之间的差异
最快的JS实现之一
lru cache 删除最近最少使用的项的缓存对象
portfinder 自动寻找 800065535内可用端口号
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和Inquirer

nodejs交互工具库 -- slash, minimist和dotenv, dotenv-expand

nodejs交互工具库 -- hash-sum, deepmerge和yaml-front-matter

nodejs交互工具库 -- resolve和semver

nodejs交互工具库 -- leven, lru cache和portfinder

nodejs交互工具库 -- ora和envinfo

nodejs交互工具库 -- memfs和execa

nodejs交互工具库 -- webpack-merge和webpack-chain

nodejs交互工具库 -- strip-ansi, address, default-gateway和joi

nodejs交互工具库 -- fs-extra, Acorn和zlib

hash-sum

非常快的唯一哈希生成器

install

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

features

  • 没有依赖关系
  • 最小的占用空间
  • 支持nodejs, io.js, 浏览器
  • 和浏览器的哈希函数基于它们的源代码
  • 为不同的对象类型产生不同的哈希,
  • 对对象中的循环引用的支持
  • 忽略了属性分配顺序

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

参考

基本常用的方法场景就这些了,更完整的用法可以直接查阅文档

hash-sum

deepmerge

深度合并两个或多个对象的可枚举属性。

UMD包是723B minified+gzipped

Getting Started

Example Usage

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
}

Installation

npm 这么做:

npm install deepmerge

deepmerge可以直接在浏览器中使用,而不需要使用包管理器/绑定器: UMD version from unpkg.com.

Include

deepmerge暴露了一个CommonJS入口点:

const merge = require('deepmerge')

ESM入口点被丢弃因为 Webpack bug.

API

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!' }

Options

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.

如果clonefalse 然后将直接复制子对象,而不是克隆。这是版本2.x之前的默认行为。

参考

基本常用的方法场景就这些了,更完整的用法可以直接查阅文档

deepmerge

Yaml Front Matter

在字符串的前面解析yaml或json。将解析后的内容和字符串内容的其余部分放入对象文字中。

Online Demo.

Breaking Changes

这个自述文件是针对4.x版本,它引入了破坏性的更改。查看 changelog 取得进一步资讯

3.x readme

Example

This

---
name: Derek Worthen
age: 127
contact:
  email: [email protected]
  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 protected]', 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: --- 需要表示前端内容的开始和结束。在开始后必须有一个换行 --- 和结束前的换行符 ---.

Install

npm

$ npm install yaml-front-matter

如果您计划使用命令行工具,请使用-g标志。

$ npm install yaml-front-matter -g

带有模块绑定的node或客户端(webpack或browsify)

var yamlFront = require('yaml-front-matter');

Browser Bundle

dist/yamlFront.js 客户端脚本将把yaml-front-matter库公开为全局, yamlFront. 客户端脚本 js-yaml 也是必需的。在某些用例中可能需要加载espirma。看 js-yaml 取得进一步资讯



Note: yaml-front-matter 是作为umd包交付的,所以它应该在commonjs, amd和浏览器(作为一个全局)环境中工作。

Running Browser Example

$ npm install --dev && npm start

然后参观 localhost:8080.

Building from source

将构建文件输出到 dist/.

$ npm install --dev && npm run build

Running Tests

npm install --dev && npm test

Command Line

Usage: yaml-front-matter [options] 

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。

Example

# Piping content from one file, through yaml parser and into another file
cat ./some/file.txt | yaml-front-matter > output.txt

JS-YAML

Yaml前端内容包装了js-yaml 以支持解析Yaml前端内容。

API

loadFront(string, [options])

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: '' }

safeLoadFront(string, [options])

api与loadFront相同,只是它不支持regexps、函数或undefined。有关更多信息,请参阅js-yaml

Options

options对象支持与 js-yaml 相同的选项,并添加了对附加键的支持。

  • options.contentKeyName: 指定用于存储未被yaml-front-matter解析的内容的对象键。默认为 __content.
yamlFront.loadFront('Hello World', {
    contentKeyName: 'fileContents' 
});
// => { fileContents: "Hello World" }

参考

基本常用的方法场景就这些了,更完整的用法可以直接查阅文档

js-yaml-front-matter

你可能感兴趣的:(javascript,前端,node.js,yarn)