javascript-interface-library v1.0.8
javascript-interface-library
various classification, validation and utility functions for JavaScript and TypeScript
From time to time, it's necessary to classify and/or validate the values of user inputs, data read from input streams (like files or network connections) or arguments passed as part of a function call. While TypeScript type annotations already eliminate the need for many of these tests, there still exist lots of interfaces to the outer (non-TypeScript) world where value checking remains important.
These situations are, what the javascript-interface-library has been made for.
NPM users: please consider the Github README for the latest description of this package (as updating the docs would otherwise always require a new NPM package version)
Just a small note: if you like this module and plan to use it, consider "starring" this repository (you will find the "Star" button on the top right of this page), so that I know which of my repositories to take most care of.
Installation
javascript-interface-library may be used as an ECMAScript module (ESM), a CommonJS or AMD module or from a global variable.
You may either install the package into your build environment using NPM with the command
npm install javascript-interface-libraryor load the plain script file directly
<script src="https://unpkg.com/javascript-interface-library"></script>Access
How to access the package depends on the type of module you prefer
- ESM (or Svelte):
import { ValueIsListSatisfying, ValueIsOrdinal } from 'javascript-interface-library' - CommonJS:
const JIL = require('javascript-interface-library') - AMD:
require(['javascript-interface-library'], (JIL) => {...})
Alternatively, you may access the global variable JIL directly.
Note for ECMAScript module users: all module functions and values are exported individually, thus allowing your bundler to perform some "tree-shaking" in order to include actually used functions or values (together with their dependencies) only.
Usage within Svelte
For Svelte, it is recommended to import the package in a module context. From then on, its exports may be used as usual:
<script context="module">
import { ValueIsListSatisfying, ValueIsOrdinal } from 'javascript-interface-library'
</script>
<script>
console.log(ValueIsListSatisfying(
[1,2,3,4], ValueIsOrdinal, 1,10
))
</script>Usage as ECMAscript, CommonJS or AMD Module (or as a global Variable)
Let's assume that you already "required" or "imported" (or simply loaded) the module according to your local environment. In that case, you may use it as follows:
console.log(JIL.ValueIsListSatisfying(
[1,2,3,4], JIL.ValueIsOrdinal, 1,10
))API Reference
As shown above, the individual functions and values may either be accessed directly (when used as an ESM) or by prefixing them with their namespace JIL (in all other cases). The following documentation lists all module contents without namespace prefix only, and the shown function signatures are those used by TypeScript.
Object Functions
The JavaScript Object class provides a few useful functions (or "static methods") for inspecting or converting a given object. Unfortunately, these functions are often used without prior checking whether the given target object actually inherits from the Object protoype or was built using Object.create(null) - and will fail whenever such a "vanilla" object is given.
JIL therefore contains the following functions which mimic their counterparts from the Object class, but succeed even if the given target object is "vanilla".
Object_hasOwnProperty (Value:Object, PropertyName:string):booleanreturnstrueif the givenValuecontains a property with the namePropertyNameas its own property - orfalseotherwise. This function mimics the JavaScript method Object.hasOwnPropertyObject_isPrototypeOf (Value:Object, Candidate:any):booleanreturnstrueif the givenValueexists in the prototype chain of a givenCandidate- orfalseotherwise. This function mimics the JavaScript method Object.isPrototypeOfObject_propertyIsEnumerable (Value:Object, PropertyName:string):booleanreturnstrueif the givenValuecontains a property with the namePropertyNameas its own property and that one is enumerable - orfalseotherwise. This function mimics the JavaScript method Object.propertyIsEnumerableObject_toString (Value:Object):stringreturns a string which represents the givenValue. This function mimics the JavaScript method Object.toStringObject_toLocaleString (Value:Object):stringreturns a locale-specific string which represents the givenValue. This function mimics the JavaScript method Object.toLocaleStringObject_valueOf (Value:Object):anyreturns the primitive value of the givenValueobject. This function mimics the JavaScript method Object.valueOf
Value Classification Functions
The following functions check whether a given argument satisfies a certain constraint (e.g., belongs to a certain category) and return either true (if the constrain is met) or false otherwise.
ValueExists (Value:any):booleanreturnstrueif the givenValueexists, i.e., if it differs from bothnullandundefined- orfalseotherwiseValueIsMissing (Value:any):booleanreturnstrueif the givenValueis eithernullorundefined- orfalseotherwiseValueIsBoolean (Value:any):booleanreturnstrueif the givenValueis either a primitive boolean value or an instance ofBoolean- orfalseotherwiseValueIsNumber (Value:any):booleanreturnstrueif the givenValueis either a primitive numeric value or an instance ofNumber- orfalseotherwiseValueIsFiniteNumber (Value:any):booleanreturnstrueif the givenValueis a finite number, i.e. a number which is notNaNand whose value is greater than negative and smaller than positive infinity - orfalseotherwiseValueIsNaN (Value:any):booleanreturnstrueif the givenValueisNaN- orfalseotherwiseValueIsNumberInRange (Value:any, minValue?:number, maxValue?:number, withMin:boolean = true, withMax:boolean = true):booleanreturnstrueif the givenValueis a number whose value is within the range given byminValueandmaxValue- orfalseotherwise.minValueis optional and defaults to negative infinity,maxValueis also optional but defaults to positive infinity. Whentrue,withMinindicates thatValuemay also be equal to the lower end of the given range, otherwise it must just be greater than the lower limit. Whentrue,withMaxindicates thatValuemay also be equal to the upper end of the given range, otherwise it must just be lower than the upper limitValueIsInteger (Value:any):booleanreturnstrueif the givenValueis a whole number - orfalseotherwiseValueIsIntegerInRange (Value:any, minValue?:number, maxValue?:number):booleanreturnstrueif the givenValueis a whole number whose value is within the range given byminValueandmaxValue- orfalseotherwise.minValueis optional and defaults to negative infinity,maxValueis also optional but defaults to positive infinityValueIsOrdinal (Value:any):booleanreturnstrueif the givenValueis a whole number greater than or equal to zero - orfalseotherwiseValueIsCardinal (Value:any):booleanreturnstrueif the givenValueis a whole number greater than or equal to one - orfalseotherwiseValueIsString (Value:any):booleanreturnstrueif the givenValueis either a primitive literal value or an instance ofString- orfalseotherwiseValueIsEmptyString (Value:any):booleanreturnstrueif the givenValueis a string without any characters or with some content that consists of white-space characters only - orfalseotherwiseValueIsNonEmptyString (Value:any):booleanreturnstrueif the givenValueis a string with some content that does not just consist of white-space characters - orfalseotherwiseValueIsStringMatching (Value:any, Pattern:RegExp):booleanreturnstrueif the givenValueis a string whose content matches the given regular expressionPattern- orfalseotherwiseValueIsText (Value:any):booleanreturnstrueif the givenValueis a string containing "ordinary" text only (i.e., a string which lacks any kind of control characters except \n or \r) - orfalseotherwiseValueIsTextline (Value:any):booleanreturnstrueif the givenValueis a string containing a single line of "ordinary" text only (i.e., a string which lacks any kind of control characters) - orfalseotherwiseValueIsFunction (Value:any):booleanreturnstrueif the givenValueis a JavaScript function - orfalseotherwiseValueIsAnonymousFunction (Value:any):booleanreturnstrueif the givenValueis an anonymous JavaScript function (i.e., a function without anameproperty) - orfalseotherwiseValueIsNamedFunction (Value:any):booleanreturnstrueif the givenValueis a "named" JavaScript function (i.e., a function with a non-emptynameproperty) - orfalseotherwiseValueIsNativeFunction (Value:any):booleanreturnstrueif the givenValueis a native JavaScript function - orfalseotherwiseValueIsScriptedFunction (Value:any):booleanreturnstrueif the givenValueis a scripted JavaScript function - orfalseotherwiseValueIsObject (Value:any):booleanreturnstrueif the givenValueis a JavaScript object (and notnull) - orfalseotherwiseValueIsPlainObject (Value:any):booleanreturnstrueif the givenValueis a JavaScript object (different fromnull) which directly inherits fromObject(such as a Javascript object literal) - orfalseotherwiseValueIsVanillaObject (Value:any):booleanreturnstrueif the givenValueis a JavaScript object which has been built usingObject.create(null)- orfalseotherwiseValueIsArray (Value:any):booleanreturnstrueif the givenValueis anArrayinstance - orfalseotherwise. If given,minLengthspecifies the minimal required list length andmaxLengthspecifies the maximal allowed list lengthValueIsList (Value:any, minLength?:number, maxLength?:number):booleanreturnstrueif the givenValueis a "dense" JavaScript array (i.e., an array whose indices 0...n-1 all exist, where n is thelengthof the given array) - orfalseotherwiseValueIsListSatisfying (Value:any, Validator:Function, minLength?:number, maxLength?:number):booleanreturnstrueif the givenValueis a "dense" JavaScript array, whose elements all pass the givenValidator- orfalseotherwise.Validatoris a function which receives a list element as its sole argument and returnstrueif the given element is "valid" orfalseotherwise. If given,minLengthspecifies the minimal required list length andmaxLengthspecifies the maximal allowed list lengthValueIsInstanceOf (Value:any, Constructor:Function):booleanreturnstrueif the givenValuewas constructed using the givenConstructorfunction - orfalseotherwiseValueInheritsFrom (Value:any, Prototype:Object):booleanreturnstrueifPrototypeexists in the prototype chain of the givenValue- orfalseotherwiseValueIsDate (Value:any):booleanreturnstrueif the givenValueis aDateinstance - orfalseotherwiseValueIsError (Value:any):booleanreturnstrueif the givenValueis anErrorinstance - orfalseotherwiseValueIsPromise (Value:any):booleanreturnstrueif the givenValueis a "Promise", i.e., an object with a property namedthenwhich contains a function - orfalseotherwiseValueIsRegExp (Value:any):booleanreturnstrueif the givenValueis aRegExpinstance - orfalseotherwiseValueIsOneOf (Value:any, ValueList:any[]):booleanreturnstrueif the givenValueequals (at least) one of the items found in the givenValueList- orfalseotherwise. Equality is checked using the JavaScript===operatorValueIsColor (Value:any):booleanreturnstrueif the givenValueis a string containing a syntactically valid CSS color specification - orfalseotherwiseValueIsEMailAddress (Value:any):booleanreturnstrueif the givenValueis a string containing a syntactically valid EMail address - orfalseotherwiseValueIsURL (Value:any):booleanreturnstrueif the givenValueis a string containing a syntactically valid URL - orfalseotherwise
Argument Validation Functions
The following functions check whether a given argument satisfies a certain constraint (e.g., belongs to a certain category) and either return the given argument (sometimes after some normalization), if the constrain is met, or throw an error otherwise.
Unless stated otherwise, these functions exist in four different "flavours", as indicated by their name prefixes:
allowXXXvalidates the given argument and returns it, if it is either missing (i.e., equalsnullorundefined) or meets the condition defined forXXX- or throws an exception otherwiseallwedXXXsynonym forallowXXX, looks better when used as an expressionexpectXXXvalidates the given argument and returns it, if it exists (i.e., differs both fromnullandundefined) and meets the condition defined forXXX- or throws an exception otherwiseexpectedXXXsynonym forexpectXXX, looks better when used as an expression
For the sake of clarity, however, only the first "flavour" (namely allowXXX) is shown in the list below (provided that this flavour actually exists).
expectValue (Description:string, Argument:any):anychecks if the givenArgumentexists (i.e., if it differs from bothnullandundefined). If this is the case, the function returns the givenArgument, otherwise an error with the message"no ${Description} given"is thrown, which uses the givenDescriptionargument. Please note: this function does not exist in the flavoursallowXXXorallowedXXXallowBoolean (Description:string, Argument:any):boolean|null|undefinedchecks if the givenArgument(if it exists) is either a primitive boolean value or an instance ofBoolean. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error with the message"the given ${Description} is no valid boolean value"is thrown, which uses the givenDescriptionallowNumber (Description:string, Argument:any):number|null|undefinedchecks if the givenArgument(if it exists) is either a primitive numeric value or an instance ofNumber. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error with the message"the given ${Description} is no valid numeric value"is thrown, which uses the givenDescriptionallowFiniteNumber (Description:string, Argument:any):number|null|undefinedchecks if the givenArgument(if it exists) is a finite number, i.e. a number which is notNaNand whose value is greater than negative and smaller than positive infinity. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowNaN (Description:string, Argument:any):number|null|undefinedchecks if the givenArgument(if it exists) isNaN. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowNumberInRange (Description:string, Argument:any, minValue?:number, maxValue?:number, withMin?:boolean, withMax?:boolean):number|null|undefinedchecks if the givenArgument(if it exists) is a number whose value is within the range given byminValueandmaxValue. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescription.minValueis optional and defaults to negative infinity,maxValueis also optional but defaults to positive infinity. When true,withMinindicates thatValuemay also be equal to the lower end of the given range, otherwise it must just be greater than the lower limit. When true,withMaxindicates thatValuemay also be equal to the upper end of the given range, otherwise it must just be lower than the upper limitallowInteger (Description:string, Argument:any):number|null|undefinedchecks if the givenArgument(if it exists) is a whole number. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowIntegerInRange (Description:string, Argument:any, minValue?:number, maxValue?:number):number|null|undefinedchecks if the givenArgument(if it exists) is a whole number whose value is within the range given byminValueandmaxValue. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescription.minValueis optional and defaults to negative infinity,maxValueis also optional but defaults to positive infinityallowOrdinal (Description:string, Argument:any):number|null|undefinedchecks if the givenArgument(if it exists) is a whole number greater than or equal to zero. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowCardinal (Description:string, Argument:any):number|null|undefinedchecks if the givenArgument(if it exists) is a whole number greater than or equal to one. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowString (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is either a primitive literal value or an instance ofString. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error with the message"the given ${Description} is no valid literal string"is thrown, which uses the givenDescriptionallowNonEmptyString (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is a string with some content that does not just consist of white-space characters. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowStringMatching (Description:string, Argument:any, Pattern:RegExp):string|null|undefinedchecks if the givenArgument(if it exists) is a string whose content matches the given regular expressionPattern. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowText (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is a string containing "ordinary" text only (i.e., a string which lacks any kind of control characters except \n or \r). If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowTextline (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is a string containing a single line of "ordinary" text only (i.e., a string which lacks any kind of control characters). If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowFunction (Description:string, Argument:any):Function|null|undefinedchecks if the givenArgument(if it exists) is a JavaScript function. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowAnonymousFunction (Description:string, Argument:any):Function|null|undefinedchecks if the givenArgument(if it exists) is an anonymous JavaScript function (i.e., a function without a name property). If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowNamedFunction (Description:string, Argument:any):Function|null|undefinedchecks if the givenArgument(if it exists) is a "named" JavaScript function (i.e., a function with a non-emptynameproperty). If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowNativeFunction (Description:string, Argument:any):Function|null|undefinedchecks if the givenArgument(if it exists) is a native JavaScript function. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowScriptedFunction (Description:string, Argument:any):Function|null|undefinedchecks if the givenArgument(if it exists) is a scripted JavaScript function. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowObject (Description:string, Argument:any):any|null|undefinedchecks if the givenArgument(if it exists) is a JavaScript object (and notnull). If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowPlainObject (Description:string, Argument:any):any|null|undefinedchecks if the givenArgument(if it exists) is a JavaScript object (different fromnull) which directly inherits fromObject(such as a Javascript object literal). If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowVanillaObject (Description:string, Argument:any):any|null|undefinedchecks if the givenArgument(if it exists) is a JavaScript object which has been built usingObject.create(null). If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowArray (Description:string, Argument:any):any[]|null|undefinedchecks if the givenArgument(if it exists) is anArrayinstance. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowList (Description:string, Argument:any, Expectation?:string,minLength?:number, maxLength?:number):any[]|null|undefinedchecks if the givenArgument(if it exists) is a "dense" JavaScript array (i.e., an array whose indices 0...n-1 all exist, where n is the length of the given array). If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescription. If given,minLengthspecifies the minimal required list length andmaxLengthspecifies the maximal allowed list lengthallowListSatisfying (Description:string, Argument:any, Validator:(Value:any) => boolean,Expectation?:string, minLength?:number, maxLength?:number):any[]|null|undefinedchecks if the givenArgument(if it exists) is a "dense" JavaScript array, whose elements all pass the givenValidator. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescription.Validatoris a function which receives a list element as its sole argument and returnstrueif the given element is "valid" orfalseotherwise. If given,minLengthspecifies the minimal required list length andmaxLengthspecifies the maximal allowed list lengthallowInstanceOf (Description:string, Argument:any, constructor:Function, Expectation:string):any|null|undefinedchecks if the givenArgument(if it exists) was constructed using the givenConstructorfunction. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowValueInheritingFrom (Description:string, Argument:any, prototype:any, Expectation:string):any|null|undefinedchecks ifPrototypeexists in the prototype chain of the givenArgument(if that exists). If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowDate (Description:string, Argument:any):Date|null|undefinedchecks if the givenArgument(if it exists) is aDateinstance. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowError (Description:string, Argument:any):Error|null|undefinedchecks if the givenArgument(if it exists) is anErrorinstance. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowPromise (Description:string, Argument:any):any|null|undefinedchecks if the givenArgument(if it exists) is a "Promise", i.e., an object with a property namedthenwhich contains a function. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowRegExp (Description:string, Argument:any):RegExp|null|undefinedchecks if the givenArgument(if it exists) is aRegExpinstance. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowOneOf (Description:string, Argument:any, ValueList:any[]):any|null|undefinedchecks if the givenArgument(if it exists) equals (at least) one of the items found in the givenValueList. If this is the case (orArgumentis missing), the function returns the givenArgument, otherwise an error is thrown whose message contains the givenDescription. Equality is checked using the JavaScript===operatorallowColor (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is a string containing a syntactically valid CSS color specification. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowEMailAddress (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is a string containing a syntactically valid EMail address. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescriptionallowURL (Description:string, Argument:any):string|null|undefinedchecks if the givenArgument(if it exists) is a string containing a syntactically valid URL. If this is the case (orArgumentis missing), the function returns the primitive value of the givenArgument, otherwise an error is thrown whose message contains the givenDescription
Utility Functions
throwableError (Message:string):Errorthis function has been provided in order to simplify the construction of "named"Errorinstances: if the givenMessagestarts with a JavaScript identifier followed by a colon, identifier and colon are stripped apart and the identifier is used as thenameproperty of a newly constructedErrorinstance for the remaining part ofMessage. Otherwise, this function is equivalent tonew Error(Message)throwError (Message:string):neverthis function has been provided in order to simplify throwing "named"Errorinstances: if the givenMessagestarts with a JavaScript identifier followed by a colon, identifier and colon are stripped apart and the identifier is used as thenameproperty of a newly constructedErrorinstance for the remaining part ofMessage. Otherwise, this function is equivalent tothrow new Error(Message)ObjectMergedWith (TargetObject:object, ...otherObjectList:object[]):objectObject.assigncan not be used to copy properties with getters and setters from one object into another - this is whatObjectMergedWithis good for: it copies the descriptors of all own properties from any object found inotherObjectListinto the givenTargetObjectand also returns that object as its function result. Any descriptor already existing for a given property inTargetObjectwill be overwrittenconstrained (Value:number, Minimum:number = -Infinity, Maximum:number = Infinity):numberlimits the givenValueto the range specified byMinimumandMaximum- i.e., the function returnsMinimumifValueis less than (or equal to)Minimum,MaximumifValueis greater than (or equal to)Maximum, orValueitself otherwise.MinimumandMaximumare optional and default to-Infinityor+Infinity, resp.escaped (Text:string):stringreturns a copy of the givenTextin which all control characters have been replaced by their corresponding escape sequencesunescaped (Text:string):stringreturns a copy of the givenTextin which all character escape sequences have been replaced by their corresponding (control) charactersquotable (Text:string, Quote:'"' | "'" = '"'):stringreturns a copy of the givenTextin which all control characters andQuotes have been replaced by their corresponding escape sequences. The outcome of this function may, f.e., used to construct literal values in JSON files.Quoteis optional and defaults to the double-quotes characterquoted (Text:string, Quote:'"' | "'" = '"'):stringreturns a copy of the givenText(embedded within a pair ofQuotes) in which all control characters andQuotes have been replaced by their corresponding escape sequences. The outcome of this function may, f.e., used to construct literal values in JSON files.Quoteis optional and defaults to the double-quotes characterHTMLsafe (Argument:string, EOLReplacement?:string):stringreturns a copy of the givenArgumentin which all control characters (except\n) and characters with a special meaning for HTML have been replaced by their corresponding HTML entities. Any linefeed characters (\n) will be replaced by the givenEOLReplacementstring - specification ofEOLReplacementis optional and defaults to<br/>MarkDownSafe (Argument:string, EOLReplacement?:string):stringreturns a copy of the givenArgumentin which all control characters (except\n) and characters with a special meaning for (HTML and) Markdown have been replaced by their corresponding HTML entities. Any linefeed characters (\n) will be replaced by the givenEOLReplacementstring - specification ofEOLReplacementis optional and defaults to<br/>ValuesDiffer (thisValue:any, otherValue:any, Mode?:'by-value'|'by-reference'|undefined):booleanreturnstrueifthisValuediffers fromotherValue- orfalseotherwise. Equality is checked by inspection, i.e.,null,undefined, booleans, strings and functions are checked using the JavaScript===operator, comparison of numbers also takes care ofNaNand a potential deviation byNumber.EPSILONand objects or arrays are (by default) compared element by element. If the optionalModeis set toby-reference, Objects are always compared by reference, if set toby-value, instances ofBoolean,NumberandStringare always compared by their primitive valueValuesAreEqual (thisValue:any, otherValue:any, Mode?:'by-value'|'by-reference'|undefined):booleanreturnstrueifthisValueequals tootherValue- orfalseotherwise. Equality is checked by inspection, i.e.,null,undefined, booleans, strings and functions are checked using the JavaScript===operator, comparison of numbers also takes care ofNaNand a potential deviation byNumber.EPSILONand objects or arrays are (by default) compared element by element. If the optionalModeis set toby-reference, Objects are always compared by reference, if set toby-value, instances ofBoolean,NumberandStringare always compared by their primitive valueObjectIsEmpty (Candidate:any):booleanreturns true if the givenCandidateis an empty object (i.e., an object without any own properties) - orfalseotherwiseObjectIsNotEmpty (Candidate:any):booleanreturns true if the givenCandidateis a non-empty empty object (i.e., an object with at least one own property) - orfalseotherwiseStringIsEmpty (Candidate:string):booleanreturns true if the givenCandidateis an empty string (i.e., a string which either contains no characters at all or only whitespace characters) - orfalseotherwiseStringIsNotEmpty (Candidate:string):booleanreturns true if the givenCandidateis a non-empty string (i.e., a string which contains one or more characters and not all of them are whitespace characters) - orfalseotherwiseValidatorForClassifier (Classifier:(Value:any) => boolean, NilIsAcceptable:boolean, Expectation:string):Functionthis function is used internally to construct many of the offered argument validation functions: it returns a new function which takes aDescriptionand anArgument, uses the givenClassifierto check ifArgumentbelongs to the expected category of values and - if it does - returns the primitive value of the givenArgument. Otherwise, an error message is constructed, which includes the givenDescriptionand complains about the given value not being a "valid ${Expectation}" - i.e.,Expectationshould describe the expected kind of argument. If set totrue,NilIsAcceptableindicates thatArgumentmay be missing (i.e.,nullorundefined), otherwise the givenArgumentis mandatory. Important Note: if you plan to develop a library which may be "tree-shaked" by application bundlers (such as WebPack and Rollup) and export functions built withValidatorForClassifier, you should mark all invocations ofValidatorForClassifieras "free of side-effects" by prepending them with/*#__PURE__*/- otherwise those invocations will remain in the bundled code even if you don't use the corresponding exportsvalidatedArgument (Description:string, Argument:any, ValueIsValid:(Value:any) => boolean,NilIsAcceptable:boolean, Expectation:string):any|null|undefinedthis function is used internally to actually validate a givenArgumentand throw anErrorwith a message containing the givenDescription, if not.ValueIsValidis the function used checkArgumentand should returntrueifArgumentis "valid" orfalseif not. If set totrue,NilIsAcceptableindicates thatArgumentmay be missing (i.e.,nullorundefined), otherwise the givenArgumentis mandatory. If validation fails, an error message is constructed, which includes the givenDescriptionand complains about the given value not being a "valid ${Expectation}" - i.e.,Expectationshould describe the expected kind of argumentFunctionWithName (originalFunction:Function, desiredName:string|String):Functionthis function is used internally to convert an anonymous functionoriginalFunctioninto a named one - either by setting thedesiredNamefor the existing function or by wrapping it into a new function with that name
Color Utilities
ColorSetis an object, whose keys are the names of all colors known by (and built into) a web browser and the corresponding values are the RGBA specifications of these colorsHexColor (Color:string):stringconverts a givenColorstring (which must be a valid CSS color specification) into the long hexadecimal format (#rrggbbaa)shortHexColor (Color:string):stringconverts a givenColorstring (which must be a valid CSS color specification) into the short hexadecimal format (#rrggbb) - such a format must be used for HTML input elements of type "color"RGBAColor (Color:string):stringconverts a givenColorstring (which must be a valid CSS color specification) into the RGBA format (rgba(r,g,b,a))
Build Instructions
You may easily build this package yourself.
Just install NPM according to the instructions for your platform and follow these steps:
- either clone this repository using git or download a ZIP archive with its contents to your disk and unpack it there
- open a shell and navigate to the root directory of this repository
- run
npm installin order to install the complete build environment - execute
npm run buildto create a new build
If you made some changes to the source code, you may also try
npm run agadooin order to check if the result is still tree-shakable.
You may also look into the author's build-configuration-study for a general description of his build environment.
License
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago