飞跃高山与大洋的鱼飞跃高山与大洋的鱼
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
首页
先看
计算机
  • 数学
  • Linux
  • Arch
  • Manjaro
  • Ubuntu
  • CentOS
  • Kubernetes
  • Web
  • JavaScript
  • TypeScript
  • CSS
  • Canvas
  • Vue
  • Vite
  • NuxtJS
  • Webpack
  • Flutter
  • D3
  • Jest
  • WeApp
  • Utils
  • Nodejs
  • Nestjs
  • Golang
  • Nginx
  • Traefik
  • MySQL
  • MongoDB
  • Redis
  • Docker
算法
  • 像素风
  • Git
  • Github
  • VSCode
  • Chrome
  • Google
  • Bookmark scripts
  • 导航 🎉
  • VuePress 侧边栏插件
  • VuePress 官网
🚇 开往
  • UTILS

    • JavaScript 工具集
    • 数组
    • 浏览器
    • 日期
    • 函数
    • 数学
    • Node
      • base64
        • 1. btoa(编码)
        • 2. atob(解码)
      • 控制台
        • 1. colorize(色彩)
      • 操作
        • 1. createDirIfNotExists(创建目录)
        • 2. JSONToFile(创建文件)
        • 3. readFileLines(读取文件行)
        • 4. untildify(替换波浪号)
      • crypto
        • 1. hashNode(哈希)
        • 2. UUIDGeneratorNode(UUID)
      • 环境
        • 1. hasFlags
        • 2. isTravisCI
      • 判断流
        • 1. isDuplexStream
        • 2. isReadableStream
        • 3. isStream
        • 4. isWritableStream
    • 对象
    • 字符串
    • 类型判断处理

Node

base64

1. btoa(编码)

FROM

30 seconds of code (Node)

FUNCTION:

const btoa = str => Buffer.from(str, 'binary').toString('base64');

EXAMPLES:

btoa('foobar1'); // 'Zm9vYmFyMQ=='

2. atob(解码)

FROM

30 seconds of code (Node)

FUNCTION:

const atob = str => Buffer.from(str, 'base64').toString('binary');

EXAMPLES:

atob('Zm9vYmFyMQ=='); // 'foobar1'1

控制台

1. colorize(色彩)

FROM

30 seconds of code (Node)

DETAIL:

colors 更详细。

FUNCTION:

const colorize = (...args) => ({
  black: `\x1b[30m${args.join(' ')}`,
  red: `\x1b[31m${args.join(' ')}`,
  green: `\x1b[32m${args.join(' ')}`,
  yellow: `\x1b[33m${args.join(' ')}`,
  blue: `\x1b[34m${args.join(' ')}`,
  magenta: `\x1b[35m${args.join(' ')}`,
  cyan: `\x1b[36m${args.join(' ')}`,
  white: `\x1b[37m${args.join(' ')}`,
  bgBlack: `\x1b[40m${args.join(' ')}\x1b[0m`,
  bgRed: `\x1b[41m${args.join(' ')}\x1b[0m`,
  bgGreen: `\x1b[42m${args.join(' ')}\x1b[0m`,
  bgYellow: `\x1b[43m${args.join(' ')}\x1b[0m`,
  bgBlue: `\x1b[44m${args.join(' ')}\x1b[0m`,
  bgMagenta: `\x1b[45m${args.join(' ')}\x1b[0m`,
  bgCyan: `\x1b[46m${args.join(' ')}\x1b[0m`,
  bgWhite: `\x1b[47m${args.join(' ')}\x1b[0m`
});

EXAMPLES:

console.log(colorize('foo').red); // 'foo' (red letters)
console.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background)
console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite); // 'foo bar' (first word in yellow letters, second word in green letters, white background for both)

操作

1. createDirIfNotExists(创建目录)

FROM

30 seconds of code (Node)

FUNCTION:

const fs = require('fs');
const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);

EXAMPLES:

createDirIfNotExists('test'); // creates the directory 'test', if it doesn't exist

2. JSONToFile(创建文件)

FROM

30 seconds of code (Node)

FUNCTION:

const fs = require('fs');
const JSONToFile = (obj, filename) =>
  fs.writeFileSync(`${filename}.json`, JSON.stringify(obj, null, 2));

EXAMPLES:

JSONToFile({ test: 'is passed' }, 'testJsonFile'); // writes the object to 'testJsonFile.json'

3. readFileLines(读取文件行)

FROM

30 seconds of code (Node)

FUNCTION:

const fs = require('fs');
const readFileLines = filename =>
  fs
    .readFileSync(filename)
    .toString('UTF8')
    .split('\n');

EXAMPLES:

/*
contents of test.txt :
  line1
  line2
  line3
  ___________________________
*/
let arr = readFileLines('test.txt');
console.log(arr); // ['line1', 'line2', 'line3']

4. untildify(替换波浪号)

FROM

30 seconds of code (Node)

FUNCTION:

const untildify = str => str.replace(/^~($|\/|\\)/, `${require('os').homedir()}$1`);

EXAMPLES:

untildify('~/node'); // '/Users/aUser/node'

crypto

1. hashNode(哈希)

FROM

30 seconds of code (Node)

FUNCTION:

const crypto = require('crypto');
const hashNode = val =>
  new Promise(resolve =>
    setTimeout(
      () =>
        resolve(
          crypto
            .createHash('sha256')
            .update(val)
            .digest('hex')
        ),
      0
    )
  );

EXAMPLES:

hashNode(JSON.stringify({ a: 'a', b: [1, 2, 3, 4], foo: { c: 'bar' } })).then(console.log); // '04aa106279f5977f59f9067fa9712afc4aedc6f5862a8defc34552d8c7206393'

2. UUIDGeneratorNode(UUID)

FROM

30 seconds of code (Node)

FUNCTION:

const crypto = require('crypto');
const UUIDGeneratorNode = () =>
  ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
    (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
  );

EXAMPLES:

UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'

环境

1. hasFlags

FROM

30 seconds of code (Node)

FUNCTION:

const hasFlags = (...flags) =>
  flags.every(flag => process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag));

EXAMPLES:

// node myScript.js -s --test --cool=true
hasFlags('-s'); // true
hasFlags('--test', 'cool=true', '-s'); // true
hasFlags('special'); // false

2. isTravisCI

FROM

30 seconds of code (Node)

FUNCTION:

const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;

EXAMPLES:

isTravisCI(); // true (if code is running on Travis CI)

判断流

1. isDuplexStream

FROM

30 seconds of code (Node)

FUNCTION:

const isDuplexStream = val =>
  val !== null &&
  typeof val === 'object' &&
  typeof val.pipe === 'function' &&
  typeof val._read === 'function' &&
  typeof val._readableState === 'object' &&
  typeof val._write === 'function' &&
  typeof val._writableState === 'object';

EXAMPLES:

const Stream = require('stream');
isDuplexStream(new Stream.Duplex()); // true

2. isReadableStream

FROM

30 seconds of code (Node)

FUNCTION:

const isReadableStream = val =>
  val !== null &&
  typeof val === 'object' &&
  typeof val.pipe === 'function' &&
  typeof val._read === 'function' &&
  typeof val._readableState === 'object';

EXAMPLES:

const fs = require('fs');
isReadableStream(fs.createReadStream('test.txt')); // true

3. isStream

FROM

30 seconds of code (Node)

FUNCTION:

const isStream = val => val !== null && typeof val === 'object' && typeof val.pipe === 'function';

EXAMPLES:

const fs = require('fs');
isStream(fs.createReadStream('test.txt')); // true

4. isWritableStream

FROM

30 seconds of code (Node)

FUNCTION:

const isWritableStream = val =>
  val !== null &&
  typeof val === 'object' &&
  typeof val.pipe === 'function' &&
  typeof val._write === 'function' &&
  typeof val._writableState === 'object';

EXAMPLES:

const fs = require('fs');
isWritableStream(fs.createWriteStream('test.txt')); // true
编辑文档!
上次更新:
贡献者: shanyuhai123
Prev
数学
Next
对象