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
/* | |
Copyright 2020 Darren Schnare | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
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
/** | |
* Get the type name for a value. | |
* | |
* @example type(null) // 'Null' | |
* @example type(undefined) // 'Undefined' | |
* @example type(45) // 'Number' | |
* @example type('Hello') // 'String' | |
* @example type({}) // 'Object' | |
* @example type(/hi/) // 'RegExp' | |
* @example type(new Date()) // 'Date' |
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
const check = (type, value) => { | |
if (typeof type !== 'string') { | |
throw new TypeError('Argument "type" must be a string') | |
} | |
const optional = /!\??$/.test(type) | |
const nullable = /\?!?$/.test(type) | |
type = type.replace(/!$|\?$/g, '').trim() | |
if (['null', 'undefined', ''].includes(type.toLowerCase())) { |
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
function validate (value, validators, message = 'Invalid value') { | |
validators = [].concat(validators) | |
if (typeof message !== 'string') { | |
throw Object.assign( | |
new Error('Argument "message" must be a string'), | |
{ | |
name: 'ArgumentError', | |
propertyName: 'message', | |
propertyValue: message | |
} |
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
/** | |
* Checks the type of a value. If the value is invalid then throws. | |
* | |
* Errors: | |
* | |
* TypeError | |
* | |
* Thrown whenever a value has an invalid type | |
* | |
* Properties: |
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
/* | |
* Simple EcmaScript templating system. | |
* | |
* Templates are just functions that accept an object of | |
* properties and return a string. | |
* | |
* const Hello = (props = {}) => ` | |
* Hello ${props.message || 'World'}! | |
* ` | |
* console.log(Hello({ message: 'Mom' })) |
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
/** | |
* A simple service container. | |
* | |
* @example | |
* const app = express() | |
* | |
* const appContainer = new ServiceContainer() | |
* const config = Object.freeze({ }) | |
* appContainer.constant('Config', config) | |
* appContainer.singleton('Db', () => new Db()) |
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
const walkObject = (function () { | |
function walkObjectRec (obj, visit, { pointer = '', key = '', visited = new WeakMap(), parent = undefined } = {}) { | |
if (obj === null || obj === undefined) { | |
visit(obj, key, pointer, parent) | |
} else if (typeof obj === 'object') { | |
if (visited.has(obj)) { | |
visited.set(obj, visited.get(obj) + 1) | |
} else { | |
visited.set(obj, 1) | |
visit(obj, key, pointer, parent) |
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
/** | |
* Read a command line option. Option names must start with '-'. | |
* | |
* Options: | |
* | |
* `required` Determines if the option must appear on the command line. | |
* | |
* `multi` Determines if the option can be specified more than once on the | |
* command line. Causes the return value to be an array. | |
* |
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
/** | |
* @param {any[]} items | |
* @param {any} item | |
* @param {{ (a: any, b: any) => number }} compare | |
* @param {number} low | |
* @param {number} high | |
* @return {number} | |
*/ | |
function binarySearch (items, item, compare, low = 0, high = items.length) { | |
if (high <= low) { |
NewerOlder