Skip to content

Bump word-wrap from 1.2.3 to 1.2.4 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 22-assert/01-ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

const assert = require('assert');

assert.ok(1, 'MSG:5');
assert.ok(true, 'MSG:6');
assert.ok('abc', 'MSG:7');

// assert.ok(0, 'MSG:9');
// assert.ok(false, 'MSG:10');

/*
* 下面的代码等价于上面,但是更简洁。
* 去掉 9, 10, 19, 20 行的代码注释,会抛出异常。
*/

assert(1, 'MSG:17');
assert(true, 'MSG:18');
assert('abc', 'MSG:19');

// assert(0, 'MSG:21');
// assert(false, 'MSG:22');
36 changes: 36 additions & 0 deletions 22-assert/02-equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node

const assert = require('assert');

assert.equal(1, 1, 'MSG:5');
assert.equal(true, true, 'MSG:6');
assert.equal('abc', 'abc', 'MSG:7');

// assert.equal(0, 1, 'MSG:9');
// assert.equal(false, true, 'MSG:10');

/*
* 下面的代码是反向断言
*/

assert.notEqual(0, 1, 'MSG:17');
assert.notEqual(false, true, 'MSG:18');
assert.notEqual('abc', 'def', 'MSG:19');

// assert.notEqual(0, 0, 'MSG:21');
// assert.notEqual(false, false, 'MSG:22');

/*
* deepEqual
*/

const a = {a: {b: 1}};
const b = {a: {b: 1}};
const c = a;

assert.equal(a, c);
assert.deepEqual(a, b);
// assert.equal(a, b, 'MSG:32');

// a, b 两个对象的地址不同,equal 比较的是地址
// 如果要比较两个对象的内容,需要用 deepEqual
18 changes: 18 additions & 0 deletions 22-assert/03-match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node

const assert = require('assert');

// assert.match('I will fail', /pass/, 'MSG:5');
// assert.match(123, /pass/, 'MSG:6');
assert.match('I will pass', /pass/, 'MSG:7');

/*
* 下面的代码是反向断言
*/

assert.doesNotMatch('I will fail', /pass/, 'MSG:13');
assert.doesNotMatch('123', /pass/, 'MSG:14');
// assert.doesNotMatch(123, /pass/, 'MSG:15');
// assert.doesNotMatch('I will pass', /pass/, 'MSG:16');

/* MSG:15 代码断言失败的原因是 123 类型不对 */
20 changes: 20 additions & 0 deletions 22-assert/04-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

const assert = require('assert');

assert.throws(
() => {
console.log('arrow function');
throw new Error('Wrong value');
},
Error,
'MSG:11'
);

// assert.throws(
// () => {
// throw new Error('Wrong value');
// },
// TypeError,
// 'MSG:19'
// );
23 changes: 23 additions & 0 deletions 22-assert/05-rejects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node

/* global Promise:true */

const assert = require('assert');

assert.rejects(
Promise.reject(new Error('Wrong value')),
Error,
'MSG:8'
);

// assert.rejects(
// Promise.reject(new Error('Wrong value')),
// TypeError,
// 'MSG:14'
// );

// assert.rejects(
// Promise.resolve(new Error('Wrong value')),
// Error,
// 'MSG:20'
// );
14 changes: 14 additions & 0 deletions 22-assert/06-if-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

const fs = require('fs');
const assert = require('assert');

fs.readFile('./01-ok.js', (err, buf) => {
assert.ifError(err);
console.log(buf.toString('utf8'));
});

// fs.readFile('xyz', (err, buf) => {
// assert.ifError(err);
// console.log(buf.toString('utf8'));
// });
16 changes: 16 additions & 0 deletions 22-assert/07-entropy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

const { h, roundFractional } = require('./lib');
const log = console.log;

log('2.34 =', roundFractional(2.34, 1));
log('h(0.5, 0.5) =', h([0.5, 0.5]));

// log('2.34 =', roundFractional(true, 1));
log('2.34 =', roundFractional(2.34, -1));

// log('h(0.5, 0.5) =', h());
// log('h(0.5, 0.5) =', h([]));
// log('h(0.5, 0.5) =', h(['0.1', '0.9']));
// log('h(0.5, 0.5) =', h([2, 0.3]));
// log('h(0.5, 0.5) =', h([0.2, 0.3]));
44 changes: 44 additions & 0 deletions 22-assert/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const assert = require('assert');

/**
* 计算信源熵
*
* h(p) = -p1*log2(p1) - p2*log2(p2) - ... - pn*log2(pn)
*
* 0 <= pi <= 1 and p1 + p2 + ... + pn = 1
*
* @param p 概率分布数组
* @returns 返回信源熵
*/
function h(p) {
assert(p instanceof Array, `${p} 不是数组类型`);
assert.notEqual(p.length, 0, '数组为空');
let total = 0;
p.forEach(pi => {
assert.equal(typeof pi, 'number', `${pi} 不是数值类型`);
assert(pi <= 1 && pi >=0, `${pi} 取值不在 [0-1] 之间`);
total += pi;
});
assert.equal(total, 1, `${p} 的概率空间不封闭`);

return p.reduce((total, pi) => total - pi * Math.log2(pi), 0);
}

/**
* 小数点后面保留第 n 位
*
* @param x 做近似处理的数
* @param n 小数点后第 n 位
* @returns 近似处理后的数
*/
function roundFractional(x, n) {
// assert(typeof x === 'number', `${x} 不是数值类型`);
// assert(typeof n === 'number', `${n} 不是数值类型`);
assert.equal(typeof x, 'number', `${x} 不是数值类型`);
assert.equal(typeof n, 'number', `${n} 不是数值类型`);
assert(n > 0, `${n} 不是正整数`);

return Math.round(x * Math.pow(10, n)) / Math.pow(10, n);
}

module.exports = { h, roundFractional };
24 changes: 0 additions & 24 deletions 22-test/todo.js

This file was deleted.

47 changes: 0 additions & 47 deletions 22-test/todo.test.js

This file was deleted.

15 changes: 15 additions & 0 deletions 25-vm/01-byte-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node

const vm = require('vm'),
fs = require('fs');

const code = fs.readFileSync('./main.js', 'utf8');
console.log(code);

const script = new vm.Script(code, {
produceCachedData: true
});
const byteCodeBuffer = script.cachedData;
console.log(byteCodeBuffer.length);
console.log(byteCodeBuffer);
fs.writeFileSync('./code.bin', byteCodeBuffer);
13 changes: 13 additions & 0 deletions 25-vm/02-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node

const vm = require('vm'),
fs = require('fs');

const byteCodeBuffer = fs.readFileSync('./code.bin');
const length = byteCodeBuffer.readIntLE(8, 4);
console.log(length);
const anotherScript = new vm.Script(
' '.repeat(length),
{cachedData: byteCodeBuffer}
);
anotherScript.runInThisContext();
Binary file added 25-vm/code.bin
Binary file not shown.
26 changes: 26 additions & 0 deletions 25-vm/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node

const log = console.log;

log(`architecture: ${process.arch}`);
log(`platform: ${process.platform}\n`);

log(`process id: ${process.pid}`);
log(`exePath: ${process.execPath}\n`);

log(`node version: ${process.version}`);
log(`user id: ${process.getuid()}`);
log(`group id: ${process.getgid()}`);
log(`cwd: ${process.cwd()}\n`);

log(`rss: ${process.memoryUsage().rss}`);
log(`heapTotal: ${process.memoryUsage().heapTotal}`);
log(`heapUsed: ${process.memoryUsage().heapUsed}`);
log(`external: ${process.memoryUsage().external}\n`);

log('env:');
log(process.env);
log(`host name: ${process.env.HOSTNAME}`);

console.log('\nApp config:');
log(process.config);
5 changes: 5 additions & 0 deletions 25-vm/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function add(a, b) {
return a + b;
}

console.log(add(2, 3));
5 changes: 5 additions & 0 deletions 26-web-assembly/01-hello-world.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

int main(int argc, char *argv[]) {
printf("hello world\n");
}
21 changes: 21 additions & 0 deletions 26-web-assembly/02-js-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

/* global WebAssembly, Uint8Array: true */
const assert = require('assert');
const {readFileSync} = require('fs');

async function main() {
const buf = new Uint8Array(readFileSync('./02-js-api.wasm'));
assert(WebAssembly.validate(buf), '非法的 wasm 文件');
const importObj = {
imports: {
imported_func: arg => console.log(arg)
}
};

const {exported_func} = await WebAssembly.instantiate(buf, importObj)
.then(res => res.instance.exports);
exported_func();
}

main();
Binary file added 26-web-assembly/02-js-api.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions 26-web-assembly/02-js-api.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "imports" "imported_func" (func (;0;) (type 0)))
(func (;1;) (type 1)
i32.const 42
call 0)
(export "exported_func" (func 1)))
Loading