Skip to content

Commit

Permalink
patch(Vest): shave off a few bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 11, 2021
1 parent 17b35eb commit 339fe45
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 78 deletions.
1 change: 1 addition & 0 deletions jsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/__shared/src/assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default Object.assign;
3 changes: 2 additions & 1 deletion packages/__shared/src/withArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import setFnName from 'setFnName';
* @return {Function}
*/
export default function withArgs(cb, fnName) {
return setFnName((...args) => {
return setFnName(function () {
const args = Array.from(arguments);
const right = args.splice(cb.length - 1);
return cb.apply(null, args.concat([right]));
}, fnName || cb.name);
Expand Down
2 changes: 1 addition & 1 deletion packages/n4s/src/compounds/allOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import runCompoundChain from 'runCompoundChain';
import withArgs from 'withArgs';

/**
* Runs a chaen of rules, making sure that all assertions pass
* Runs a chain of rules, making sure that all assertions pass
*
* @param {EnforceContext} value
* @param {[{test: Function, run: Function}]} ruleChains
Expand Down
4 changes: 3 additions & 1 deletion packages/n4s/src/runtime/EnforceContext.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import assign from 'assign';

/**
* Stores values and configuration passed down to compound rules.
*
* @param {Object} content
*/
export default function EnforceContext(content) {
Object.assign(this, content);
assign(this, content);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/n4s/src/runtime/RuleResult.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hasOwnProperty from 'hasOwnProperty';

import assign from 'assign';
import { isBoolean } from 'isBoolean';
import { isEmpty } from 'isEmpty';
import { isNull } from 'isNull';
Expand Down Expand Up @@ -113,7 +114,7 @@ RuleResult.prototype.extend = function (newRes) {

const children = mergeChildren(res, this).children;

Object.assign(this, res);
assign(this, res);
if (!isEmpty(children)) {
this.children = children;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/n4s/src/runtime/enforce.extend.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import assign from 'assign';
import bindLazyRule from 'bindLazyRule';
import genRuleProxy from 'genRuleProxy';
import proxySupported from 'proxySupported';
import runtimeRules from 'runtimeRules';

export default function bindExtend(enforce, Enforce) {
enforce.extend = customRules => {
Object.assign(runtimeRules, customRules);
assign(runtimeRules, customRules);

if (!proxySupported()) {
genRuleProxy(Enforce, bindLazyRule);
Expand Down
3 changes: 2 additions & 1 deletion packages/n4s/src/runtime/runtimeRules.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assign from 'assign';
import compounds from 'compounds';
import ruleMeta from 'ruleMeta';
import rules from 'rules';

export default Object.assign(rules(), compounds, ruleMeta);
export default assign(rules(), compounds, ruleMeta);
6 changes: 2 additions & 4 deletions packages/vest/docs/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ import vest, { test, enforce } from 'vest';
import any from 'vest/any';

vest.create('Checkout', () => {
test(
'coupon',
'When filled, must be at least 5 chars',
() => any(
test('coupon', 'When filled, must be at least 5 chars', () =>
any(
() => enforce(data.coupon).isEmpty(),
() => enforce(data.coupon).longerThanOrEquals(5)
)
Expand Down
3 changes: 2 additions & 1 deletion packages/vest/src/core/ctx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createContext from 'context';

import assign from 'assign';
import {
EXCLUSION_ITEM_TYPE_TESTS,
EXCLUSION_ITEM_TYPE_GROUPS,
Expand All @@ -8,7 +9,7 @@ import {
const context = createContext((ctxRef, parentContext) =>
parentContext
? null
: Object.assign(
: assign(
{},
{
exclusion: {
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/produce/getFailuresByGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import throwError from 'throwError';

/**
* Gets failure messages by group.
* @param {'warn'|'error'} severityKey Severity filter.
* @param {'errors'|'warnings'} severityKey lookup severity
* @param {string} group Group name.
* @param {string} [fieldName] Field name.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/produce/hasFailuresByGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTestObjects } from 'stateHooks';

/**
* Checks whether there are failures in a given group.
* @param {'warn'|'error'} severityKey Severity filter.
* @param {'errors'|'warnings'} severityKey lookup severity
* @param {string} group Group name.
* @param {string} [fieldName] Field name.
* @return {boolean}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ Object {
"pending": Object {
"lagging": Array [],
"pending": Array [
VestTest {
Object {
"asyncTest": Promise {},
"cancel": [Function],
"fail": [Function],
"failed": false,
"fieldName": "field_1",
"id": "0",
"isWarning": false,
"statement": "some statement string",
"testFn": [Function],
"valueOf": [Function],
"warn": [Function],
},
],
},
Expand Down
3 changes: 0 additions & 3 deletions packages/vest/src/core/test/__tests__/each.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import faker from 'faker';

import VestTest from 'VestTest';
import enforce from 'enforce';
import test from 'test';
import vest from 'vest';
Expand All @@ -22,8 +21,6 @@ describe("Test Vest's `test.each` function", () => {
expect(testObjects).toHaveLength(2);
expect(testObjects[0].failed).toBe(false);
expect(testObjects[1].failed).toBe(false);
expect(testObjects[0]).toBeInstanceOf(VestTest);
expect(testObjects[1]).toBeInstanceOf(VestTest);
});

it('Should mark failed tests as such', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/__tests__/memo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('test.memo', () => {
expect(newRes).toMatchSnapshot();
});

it('Should return new VestTest instances', () => {
it('Should return testObject', () => {
validate(2);
expect(test1Set.size).toBe(2);
expect(test2Set.size).toBe(2);
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/core/test/__tests__/runAsyncTest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe.each([CASE_PASSING /*, CASE_FAILING*/])(
return state;
});
});
testObject = new VestTest({
testObject = VestTest({
fieldName,
statement: STATEMENT,
testFn: () => null,
Expand Down Expand Up @@ -120,7 +120,7 @@ describe.each([CASE_PASSING /*, CASE_FAILING*/])(
beforeEach(() => {
context.run({ stateRef }, () => {
setPending(
new VestTest({
VestTest({
fieldName: 'pending_field',
statement: STATEMENT,
testFn: jest.fn(),
Expand Down
68 changes: 37 additions & 31 deletions packages/vest/src/core/test/lib/VestTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,55 @@ import removeTestFromState from 'removeTestFromState';
* @param {string} [group] The group in which the test runs.
*/
function VestTest({ fieldName, statement, testFn, group }) {
Object.assign(this, {
const testObject = {
cancel,
fail,
failed: false,
fieldName,
id: id(),
isWarning: false,
statement,
testFn,
});
valueOf,
warn,
};

if (group) {
this.groupName = group;
testObject.groupName = group;
}
}

/**
* @returns {Boolean} Current validity status of a test.
*/
VestTest.prototype.valueOf = function () {
return this.failed !== true;
};
return testObject;

/**
* Sets a test to failed.
*/
VestTest.prototype.fail = function () {
this.failed = true;
};
/**
* @returns {Boolean} Current validity status of a test.
*/
function valueOf() {
return testObject.failed !== true;
}

/**
* Sets a current test's `isWarning` to true.
*/
VestTest.prototype.warn = function () {
this.isWarning = true;
};
/**
* Sets a test to failed.
*/
function fail() {
testObject.failed = true;
}

/**
* Marks a test as canceled, removes it from the state.
* This function needs to be called within a stateRef context.
*/
VestTest.prototype.cancel = function () {
this.canceled = true;
removePending(this);
removeTestFromState(this);
};
/**
* Sets a current test's `isWarning` to true.
*/
function warn() {
testObject.isWarning = true;
}

/**
* Marks a test as canceled, removes it from the state.
* This function needs to be called within a stateRef context.
*/
function cancel() {
testObject.canceled = true;
removePending(this);
removeTestFromState(this);
}
}

export default VestTest;
9 changes: 4 additions & 5 deletions packages/vest/src/core/test/lib/__tests__/VestTest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('VestTest', () => {
let testObject;

beforeEach(() => {
testObject = new VestTest({
testObject = VestTest({
fieldName,
statement,
testFn: jest.fn(),
Expand All @@ -29,9 +29,8 @@ describe('VestTest', () => {
});

it('Should have a unique id', () => {
Array.from(
{ length: 100 },
() => new VestTest({ fieldName, statement, testFn: jest.fn() })
Array.from({ length: 100 }, () =>
VestTest({ fieldName, statement, testFn: jest.fn() })
).reduce((existing, { id }) => {
expect(existing[id]).toBeUndefined();
existing[id] = true;
Expand All @@ -53,7 +52,7 @@ describe('VestTest', () => {
jest.resetModules();

const VestTest = require('VestTest');
testObject = new VestTest({
testObject = VestTest({
fieldName,
statement,
testFn: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`VestTest TestObject constructor 1`] = `
VestTest {
Object {
"cancel": [Function],
"fail": [Function],
"failed": false,
"fieldName": "unicycle",
"id": "0",
"isWarning": false,
"statement": "I am Root.",
"testFn": [MockFunction],
"valueOf": [Function],
"warn": [Function],
}
`;

exports[`VestTest testObject.warn Should set \`.isWarning\` to true 1`] = `
VestTest {
Object {
"cancel": [Function],
"fail": [Function],
"failed": false,
"fieldName": "unicycle",
"id": "102",
"isWarning": true,
"statement": "I am Root.",
"testFn": [MockFunction],
"valueOf": [Function],
"warn": [Function],
}
`;

0 comments on commit 339fe45

Please sign in to comment.