-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: reduce transpiled bundle size (#522)
- Loading branch information
Showing
17 changed files
with
144 additions
and
121 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function setFnName(fn, value) { | ||
return Object.defineProperty(fn, 'name', { value }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import setFnName from 'setFnName'; | ||
|
||
/** | ||
* ES5 Transpilation increases the size of spread arguments by a lot. | ||
* Wraps a function and passes its spread params as an array. | ||
* | ||
* @param {Function} cb | ||
* @param {String} [fnName] | ||
* @return {Function} | ||
*/ | ||
export default function withAgs(cb, fnName) { | ||
return setFnName((...args) => cb(args), fnName || cb.name); | ||
} | ||
|
||
/** | ||
* Spreads all the passed arguments, and forwards them as | ||
* first arg, and the rest as an array. | ||
* | ||
* @param {Function} cb | ||
*/ | ||
export function withFirst(cb) { | ||
return withAgs(args => cb(args[0], args.slice(1)), cb.name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
|
||
import { RUN_RULE } from 'enforceKeywords'; | ||
import genRuleProxy from 'genRuleProxy'; | ||
import isFunction from 'isFunction'; | ||
import runtimeRules from 'runtimeRules'; | ||
import setFnName from 'setFnName'; | ||
import withArgs from 'withArgs'; | ||
|
||
// Initiates a chain of functions directly from the `enforce` | ||
// function - that's even though we do not have any closure | ||
// there to store that data. | ||
export default function bindLazyRule(ruleName) { | ||
const registeredRules = []; | ||
|
||
const addFn = fnName => (...args) => { | ||
registeredRules.push( | ||
Object.defineProperty( | ||
value => runtimeRules[fnName](value, ...args), | ||
'name', | ||
{ value: fnName } | ||
) | ||
); | ||
const addFn = fnName => | ||
withArgs(args => { | ||
registeredRules.push( | ||
setFnName(value => runtimeRules[fnName](value, ...args), fnName) | ||
); | ||
|
||
const returnvalue = genRuleProxy({}, addFn); | ||
const returnvalue = genRuleProxy({}, addFn); | ||
|
||
return Object.assign(returnvalue, { | ||
[RUN_RULE]: getValue => { | ||
return registeredRules.every(fn => { | ||
try { | ||
// This inversion of control when getting the value is | ||
// required in order to pass the function over to `shape` | ||
// so it can make the decision which args to pass to `optional` | ||
return !!fn(isFunction(getValue) ? getValue(fn.name) : getValue); | ||
} catch (e) { | ||
return false; | ||
} | ||
}); | ||
}, | ||
return Object.assign(returnvalue, { | ||
[RUN_RULE]: getValue => { | ||
return registeredRules.every(fn => { | ||
try { | ||
// This inversion of control when getting the value is | ||
// required in order to pass the function over to `shape` | ||
// so it can make the decision which args to pass to `optional` | ||
return !!fn(isFunction(getValue) ? getValue(fn.name) : getValue); | ||
} catch (e) { | ||
return false; | ||
} | ||
}); | ||
}, | ||
}); | ||
}); | ||
}; | ||
|
||
return addFn(ruleName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import runLazyRules from 'runLazyRules'; | ||
import { withFirst } from 'withArgs'; | ||
|
||
/** | ||
* @param {*} value Value to be test against rules | ||
* @param {Function[]} rules Rules to validate the value with | ||
*/ | ||
export default function anyOf(value, ...rules) { | ||
return !(rules.length) || rules.some(ruleGroup => runLazyRules(ruleGroup, value)); | ||
function anyOf(value, rules) { | ||
return ( | ||
!rules.length || rules.some(ruleGroup => runLazyRules(ruleGroup, value)) | ||
); | ||
} | ||
|
||
export default withFirst(anyOf); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { isNotArray } from 'isArray'; | ||
import runLazyRules from 'runLazyRules'; | ||
import { withFirst } from 'withArgs'; | ||
|
||
export default function isArrayOf(value, ...ruleChain) { | ||
function isArrayOf(value, ruleChain) { | ||
if (isNotArray(value)) { | ||
return false; | ||
} | ||
return value.every(element => | ||
ruleChain.some(rule => runLazyRules(rule, element)) | ||
); | ||
} | ||
|
||
export default withFirst(isArrayOf); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.