Skip to content

Commit

Permalink
feat: migrate from rollup to ncc
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed May 12, 2020
1 parent 40e02f2 commit 91732e4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 75 deletions.
9 changes: 1 addition & 8 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"env": {
"test": {
"presets": [["@babel/preset-env", {"loose": true}]]
},
"development": {
"presets": [["@babel/preset-env", {"loose": true, "modules": false}]]
}
}
"presets": [["@babel/preset-env", {"loose": true}]]
}
23 changes: 10 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,25 @@
"license": "MIT",
"author": "Chunpeng Huo",
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"@babel/register": "^7.8.6",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/register": "^7.9.0",
"@zeit/ncc": "^0.22.1",
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"rollup": "^2.0.6",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.3.0",
"standard-changelog": "^2.0.21",
"eslint": "^7.0.0",
"standard-changelog": "^2.0.24",
"tap-nirvana": "^1.1.0",
"tape": "^4.13.2"
"tape": "^5.0.0"
},
"scripts": {
"build": "env BABEL_ENV=development rollup -c",
"build": "ncc build src/index.js -m",
"lint": "eslint src",
"prepare": "npm run build",
"preversion": "npm test",
"version": "standard-changelog && git add CHANGELOG.md",
"postversion": "git push && git push --tags && npm publish",
"pretest": "npm run lint",
"test": "env BABEL_ENV=test tape -r @babel/register 'test/**/*.spec.js' | tap-nirvana"
"test": "tape -r @babel/register 'test/**/*.spec.js' | tap-nirvana"
},
"main": "dist/index.js",
"types": "index.d.ts",
Expand All @@ -46,7 +43,7 @@
"index.d.ts"
],
"dependencies": {
"bcx-expression-evaluator": "^1.1.0",
"bcx-expression-evaluator": "^1.2.0",
"lodash": "^4.17.15"
}
}
32 changes: 16 additions & 16 deletions test/standard-validator-wrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ import {createSimpleScope} from 'bcx-expression-evaluator';
const w = standardValidatorWrap;

test('standardValidatorWrap: normalizes validator output to ValidationResult', t => {
t.deepEqual(w(() => undefined)(), {isValid: true});
t.deepEqual(w(() => "foo")(), {isValid: false, errors: ['foo']});
t.deepEqual(w(() => ['foo', 'bar'])(), {isValid: false, errors: ['foo', 'bar']});
t.deepLooseEqual(w(() => undefined)(), {isValid: true});
t.deepLooseEqual(w(() => "foo")(), {isValid: false, errors: ['foo']});
t.deepLooseEqual(w(() => ['foo', 'bar'])(), {isValid: false, errors: ['foo', 'bar']});
t.end();
});

test('standardValidatorWrap: forces break', t => {
t.deepEqual(w(() => undefined, {stopValidationChainIfPass: true})(), {isValid: true, break: true});
t.deepEqual(w(() => undefined, {stopValidationChainIfFail: true})(), {isValid: true});
t.deepEqual(w(() => "foo", {stopValidationChainIfPass: true})(), {isValid: false, errors: ['foo']});
t.deepEqual(w(() => "foo", {stopValidationChainIfFail: true})(), {isValid: false, errors: ['foo'], break: true});
t.deepEqual(w(() => ['foo', 'bar'], {stopValidationChainIfPass: true})(), {isValid: false, errors: ['foo', 'bar']});
t.deepEqual(w(() => ['foo', 'bar'], {stopValidationChainIfFail: true})(), {isValid: false, errors: ['foo', 'bar'], break: true});
t.deepLooseEqual(w(() => undefined, {stopValidationChainIfPass: true})(), {isValid: true, break: true});
t.deepLooseEqual(w(() => undefined, {stopValidationChainIfFail: true})(), {isValid: true});
t.deepLooseEqual(w(() => "foo", {stopValidationChainIfPass: true})(), {isValid: false, errors: ['foo']});
t.deepLooseEqual(w(() => "foo", {stopValidationChainIfFail: true})(), {isValid: false, errors: ['foo'], break: true});
t.deepLooseEqual(w(() => ['foo', 'bar'], {stopValidationChainIfPass: true})(), {isValid: false, errors: ['foo', 'bar']});
t.deepLooseEqual(w(() => ['foo', 'bar'], {stopValidationChainIfFail: true})(), {isValid: false, errors: ['foo', 'bar'], break: true});
t.end();
});

test('standardValidatorWrap: overrides error message', t => {
const s = createSimpleScope({});
t.deepEqual(w(() => "foo", {message: 'bar'})(s), {isValid: false, errors: ['bar']});
t.deepLooseEqual(w(() => "foo", {message: 'bar'})(s), {isValid: false, errors: ['bar']});
t.end();
});

test('standardValidatorWrap: overrides error message with string interoperation', t => {
const s = createSimpleScope({});
s.overrideContext.$value = 'test';
t.deepEqual(w({
t.deepLooseEqual(w({
$patchScope: scope => scopeVariation(scope, {$value: 'lorem'}),
$validator: () => "foo"
}, {message: "${$value} bar ${$errors.join(',')}"})(s), {isValid: false, errors: ['test bar foo']});
Expand All @@ -43,7 +43,7 @@ test('standardValidatorWrap: overrides error message with string interoperation'
test('standardValidatorWrap: overrides error message with function', t => {
const s = createSimpleScope({});
s.overrideContext.$value = 'test';
t.deepEqual(w(() => "foo", {message: v => v + ' is invalid'})(s), {isValid: false, errors: ['test is invalid']});
t.deepLooseEqual(w(() => "foo", {message: v => v + ' is invalid'})(s), {isValid: false, errors: ['test is invalid']});
t.end();
});

Expand All @@ -65,9 +65,9 @@ test('standardValidatorWrap: patches scope', t => {
good.overrideContext.$value = 'good';
const bad = createSimpleScope({});
bad.overrideContext.$value = 'bad';
t.deepEqual(w(validator)(good), {isValid: true});
t.deepEqual(w(validator, {message: 'length of ${$value} is less than ${$min}'})(good), {isValid: true});
t.deepEqual(w(validator)(bad), {isValid: false, errors: ['foo']});
t.deepEqual(w(validator, {message: '${$errors.join(",")}: length of ${$value} is less than ${$min}'})(bad), {isValid: false, errors: ['foo: length of bad is less than 4']});
t.deepLooseEqual(w(validator)(good), {isValid: true});
t.deepLooseEqual(w(validator, {message: 'length of ${$value} is less than ${$min}'})(good), {isValid: true});
t.deepLooseEqual(w(validator)(bad), {isValid: false, errors: ['foo']});
t.deepLooseEqual(w(validator, {message: '${$errors.join(",")}: length of ${$value} is less than ${$min}'})(bad), {isValid: false, errors: ['foo: length of bad is less than 4']});
t.end();
});
76 changes: 38 additions & 38 deletions test/validation-result.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,61 @@ function v(result) {
}

test('ValidationResult: undefined or null means pass', t => {
t.deepEqual(v(), {isValid: true});
t.deepEqual(v(null), {isValid: true});
t.deepLooseEqual(v(), {isValid: true});
t.deepLooseEqual(v(null), {isValid: true});
t.end();
});

test('ValidationResult: boolean means pass or fail', t => {
t.deepEqual(v(true), {isValid: true});
t.deepEqual(v(false), {isValid: false, errors: ['invalid']});
t.deepLooseEqual(v(true), {isValid: true});
t.deepLooseEqual(v(false), {isValid: false, errors: ['invalid']});
t.end();
});

test('ValidationResult: non-empty string means error message', t => {
t.deepEqual(v(""), {isValid: true});
t.deepEqual(v("bar"), {isValid: false, errors: ["bar"]});
t.deepLooseEqual(v(""), {isValid: true});
t.deepLooseEqual(v("bar"), {isValid: false, errors: ["bar"]});
t.end();
});

test('ValidationResult: array of non-empty string means error messages, remove duplicated messages', t => {
t.deepEqual(v(["", "", ""]), {isValid: true});
t.deepEqual(v(["", "bar"]), {isValid: false, errors: ["bar"]});
t.deepEqual(v(["foo", "", "bar", "lorem", "foo"]), {isValid: false, errors: ["foo", "bar", "lorem"]});
t.deepLooseEqual(v(["", "", ""]), {isValid: true});
t.deepLooseEqual(v(["", "bar"]), {isValid: false, errors: ["bar"]});
t.deepLooseEqual(v(["foo", "", "bar", "lorem", "foo"]), {isValid: false, errors: ["foo", "bar", "lorem"]});
t.end();
});

test('ValidationResult: can set inValid and message(s) explictly, remove duplicated messages', t => {
t.deepEqual(v({isValid: true}), {isValid: true});
t.deepEqual(v({isValid: true, break: true}), {isValid: true, break: true});
t.deepLooseEqual(v({isValid: true}), {isValid: true});
t.deepLooseEqual(v({isValid: true, break: true}), {isValid: true, break: true});

t.deepEqual(v({isValid: true, message: 'ignore'}), {isValid: true});
t.deepEqual(v({isValid: true, messages: ['ignore']}), {isValid: true});
t.deepLooseEqual(v({isValid: true, message: 'ignore'}), {isValid: true});
t.deepLooseEqual(v({isValid: true, messages: ['ignore']}), {isValid: true});

t.deepEqual(v({isValid: false, message: 'bar'}), {isValid: false, errors: ["bar"]});
t.deepEqual(v({isValid: false, message: 'bar', break: true}), {isValid: false, errors: ["bar"], break: true});
t.deepEqual(v({isValid: false, message: ''}), {isValid: false, errors: ["invalid"]});
t.deepEqual(v({isValid: false}), {isValid: false, errors: ["invalid"]});
t.deepEqual(v({isValid: false, messages: ['foo', 'bar', '', 'foo']}), {isValid: false, errors: ['foo', 'bar']});
t.deepLooseEqual(v({isValid: false, message: 'bar'}), {isValid: false, errors: ["bar"]});
t.deepLooseEqual(v({isValid: false, message: 'bar', break: true}), {isValid: false, errors: ["bar"], break: true});
t.deepLooseEqual(v({isValid: false, message: ''}), {isValid: false, errors: ["invalid"]});
t.deepLooseEqual(v({isValid: false}), {isValid: false, errors: ["invalid"]});
t.deepLooseEqual(v({isValid: false, messages: ['foo', 'bar', '', 'foo']}), {isValid: false, errors: ['foo', 'bar']});
t.end();
});

test('ValidationResult: can skip inValid explictly', t => {
t.deepEqual(v({isValid: null}), {isValid: null});
t.deepEqual(v({isValid: null, break: true}), {isValid: null, break: true});
t.deepLooseEqual(v({isValid: null}), {isValid: null});
t.deepLooseEqual(v({isValid: null, break: true}), {isValid: null, break: true});
t.end();
});

test('ValidationResult: handles tri-state isValid', t => {
t.deepEqual(v([{isValid: true}, {isValid: true}]), {isValid: true});
t.deepEqual(v([{isValid: null}, {isValid: null}]), {isValid: null});
t.deepEqual(v([{isValid: false}, {isValid: false}]), {isValid: false, errors: ['invalid']});
t.deepEqual(v([{isValid: null}, {isValid: true}]), {isValid: true});
t.deepEqual(v([{isValid: true}, {isValid: null}]), {isValid: true});
t.deepEqual(v([{isValid: true}, {isValid: false}]), {isValid: false, errors: ['invalid']});
t.deepEqual(v([{isValid: false}, {isValid: true}]), {isValid: false, errors: ['invalid']});
t.deepEqual(v([{isValid: null}, {isValid: false}]), {isValid: false, errors: ['invalid']});
t.deepEqual(v([{isValid: false}, {isValid: null}]), {isValid: false, errors: ['invalid']});
t.deepLooseEqual(v([{isValid: true}, {isValid: true}]), {isValid: true});
t.deepLooseEqual(v([{isValid: null}, {isValid: null}]), {isValid: null});
t.deepLooseEqual(v([{isValid: false}, {isValid: false}]), {isValid: false, errors: ['invalid']});
t.deepLooseEqual(v([{isValid: null}, {isValid: true}]), {isValid: true});
t.deepLooseEqual(v([{isValid: true}, {isValid: null}]), {isValid: true});
t.deepLooseEqual(v([{isValid: true}, {isValid: false}]), {isValid: false, errors: ['invalid']});
t.deepLooseEqual(v([{isValid: false}, {isValid: true}]), {isValid: false, errors: ['invalid']});
t.deepLooseEqual(v([{isValid: null}, {isValid: false}]), {isValid: false, errors: ['invalid']});
t.deepLooseEqual(v([{isValid: false}, {isValid: null}]), {isValid: false, errors: ['invalid']});
t.end();
});

Expand All @@ -70,7 +70,7 @@ test('ValidationResult: surfaces out final break in chain', t => {
];

// the first break is not final break
t.deepEqual(v(chain), {isValid: false, errors: ['foo']});
t.deepLooseEqual(v(chain), {isValid: false, errors: ['foo']});

const chain2 = [
{isValid: true},
Expand All @@ -79,7 +79,7 @@ test('ValidationResult: surfaces out final break in chain', t => {

// final break
chain2.break = true;
t.deepEqual(v(chain2), {isValid: false, errors: ['foo'], break: true});
t.deepLooseEqual(v(chain2), {isValid: false, errors: ['foo'], break: true});
t.end();
});

Expand All @@ -98,7 +98,7 @@ test('ValidationResult: processes nested result, remove duplicated messages', t
new ValidationResult("last")
];

t.deepEqual(v(result), {
t.deepLooseEqual(v(result), {
isValid: false,
errors: ["foo", "bar", "bar1", "inner", "hello", "last"]
});
Expand All @@ -118,8 +118,8 @@ test('ValidationResult: rejects unpected result', t => {
test('ValidationResult: double wrap has no effect', t => {
const p = v({isValid: true, message: 'ignore'});
const f = v({isValid: false, message: 'bar', break: true});
t.deepEqual(v(p), {isValid: true});
t.deepEqual(v(f), {isValid: false, errors: ['bar'], break: true});
t.deepLooseEqual(v(p), {isValid: true});
t.deepLooseEqual(v(f), {isValid: false, errors: ['bar'], break: true});
t.end();
});

Expand All @@ -129,7 +129,7 @@ test('ValidationResult: merge nested errors', t => {
{isValid: false, errors: {a: ['bar'], b: ['foo']}}
];

t.deepEqual(v(result), {
t.deepLooseEqual(v(result), {
isValid: false,
errors: {a: ['bar'], b: ['foo']}
});
Expand All @@ -140,7 +140,7 @@ test('ValidationResult: merge nested errors', t => {
{isValid: false, errors: {a: ['bar', 'goo'], b: ['foo']}}
];

t.deepEqual(v(result), {
t.deepLooseEqual(v(result), {
isValid: false,
errors: {__base__: ['bar'], a: ['bar', 'goo'], b: ['foo']}
});
Expand All @@ -151,7 +151,7 @@ test('ValidationResult: merge nested errors', t => {
{isValid: false, errors: {a: ['bar', 'goo'], b: ['foo']}}
];

t.deepEqual(v(result), {
t.deepLooseEqual(v(result), {
isValid: false,
errors: {__base__: ['bar', 'foo'], a: ['bar', 'goo'], b: ['foo']}
});
Expand All @@ -172,7 +172,7 @@ test('ValidationResult: merge deep nested errors', t => {
]
];

t.deepEqual(v(result), {
t.deepLooseEqual(v(result), {
isValid: false,
errors: {__base__: ['foo', 'goo', 'bar', 'xyz'], a: ['bar', 'goo'], b: ['goo', 'foo']}
});
Expand Down

0 comments on commit 91732e4

Please sign in to comment.