#Huz Api / Scalar Type
Handles primitive/scalar types as plain, array, map or map of
Commands
npm run clear
Clears dist folder
npm run lint
Runs eslint for static code analysis - CleanCode
npm run test
Runs test files - Test-Driven
npm run build
Builds JS files at dist folder
npm run start
Runs main file in dist folder (dist/index.js)
npm publish
Publishes dist folder to npm
Install
npm i @huzapi/scalar-type
Import
const {arrayType, objectType} = require('@huzapi/scalar-type');
Shared Options
Name | Type | Default | cast | castArray | castMap | Info |
---|---|---|---|---|---|---|
log | boolean | true | [x] | [x] | [x] | logs when a problem |
error | boolean | false | [x] | [x] | [x] | throws when a problem |
mandatory | boolean | false | [x] | [o] | [o] | problem if null |
unique | boolean | false | [o] | [x] | [o] | removes duplicated items in array |
keepNullItems | boolean | false | [o] | [x] | [o] | keeps null items in array (default: remove nulls) |
minItems | integer | null | [o] | [x] | [o] | problem if array size is less than |
maxItems | integer | null | [o] | [x] | [o] | problem if array size is greater than |
keepNullValues | boolean | false | [o] | [o] | [x] | keeps null values in map (default: remove nulls) |
minKeys | integer | null | [o] | [o] | [x] | problem if object key size is less than |
maxKeys | integer | null | [o] | [o] | [x] | problem if object key size is greater than |
Types
stringType/codeType/namerType/uuidType
- Returns a string in any event ==> string|null
- Trims texts (else noTrim:true)
- Converts empty string to null (else noTrim:true)
Specific Options
Name | Type | Default | cast | castArray | castMap | Info |
---|---|---|---|---|---|---|
noTrim | boolean | false | [x] | [x] | [x] | ignore trim (except uuidType) |
minLength | integer | null | [x] | [x] | [x] | problem if text length is less than (except uuidType) |
maxLength | integer | null | [x] | [x] | [x] | problem if text length is greater than (except uuidType) |
pattern | string | null | [x] | [x] | [x] | for only codeType and namerType |
Most Used Function
- stringType.is(value: any): boolean
- stringType.cast(value: any, opt?: Options, req?: Request): string|null
- stringType.castArray(value: any, opt?: Options, req?: Request): Array<string> // or Array<string|null> with keepNullItems:true
- stringType.castMap(value: any, opt?: Options, req?: Request): Record<string, string> // or Record<string, string|null> with keepNullValues:true
- stringType.castMapOf(value: any, opt?: Options, req?: Request): Record<string, any>
Samples
Call | Result |
---|---|
stringType.cast(undefined ) |
null |
stringType.cast(5 ) |
"5" |
stringType.cast(true ) |
"true" |
stringType.cast(" " ) |
null |
stringType.cast(" foo bar " ) |
"foo bar" |
stringType.cast([" foo"] ) |
"foo" |
stringType.cast({id:5} ) |
"5" id property is special
|
stringType.cast(() => 5 ) |
"5" |
stringType.cast(" foo bar " , {noTrim:true}) |
" foo bar " |
stringType.cast("foo bar" , {error: true, minLength:10}) |
throws |
stringType.cast("foo bar" , {minLength:10}) |
logs |
stringType.cast(" " , {error: true, mandatory:true}) |
throws |
stringType.cast({a:1} ) |
logs |
stringType.castArray(null ) |
[] |
stringType.castArray("foo bar " ) |
["foo bar"] |
stringType.castArray([5,true," foo bar", () => 4.5, {id:5}, null] ) |
["5", "true", "foo bar", "4.5", "5"] |
stringType.castMap(null ) |
{} |
stringType.castMap({k1: 5, k2: true, k3: " foo bar", k4: () => 4.5, k5: {id:5}, k6: null} ) |
{k1: "5", k2: "true", k3: "foo bar", k4: "4.5", k5: "5"} |
intType/floatType
- Returns a number in any event ==> number|null
- Converts infinite numbers to null
- Converts nan numbers to null
- Floors if number is float for only intType
Specific Options
Name | Type | Default | cast | castArray | castMap | Info |
---|---|---|---|---|---|---|
def | integer | null | [x] | [x] | [x] | if null use default value |
min | integer | null | [x] | [x] | [x] | problem if value is less than |
max | integer | null | [x] | [x] | [x] | problem if value is greater than |
Most Used Function
- intType.is(value: any): boolean
- intType.cast(value: any, opt?: Options, req?: Request): number|null
- intType.castArray(value: any, opt?: Options, req?: Request): Array<number> // or Array<number|null> with keepNullItems:true
- intType.castMap(value: any, opt?: Options, req?: Request): Record<string, number> // or Record<string, number|null> with keepNullValues:true
- intType.castMapOf(value: any, opt?: Options, req?: Request): Record<number, any>
Samples
Call | Result |
---|---|
intType.cast(undefined ) |
null |
intType.cast("foo" , {def:4}) |
4 |
intType.cast("5" ) |
5 |
intType.cast(true ) |
1 |
intType.cast(false ) |
0 |
intType.cast({id:5} ) |
5 id property is special
|
intType.cast(() => 5 ) |
5 |
intType.cast([5] ) |
5 |
intType.cast(5.3 ) |
5 |
intType.cast(" foo bar " , {def: 5}) |
5 |
intType.cast("foo" , {def: 5}) |
5 |
intType.cast("foo" , {mandatory:true}) |
logs |
intType.cast("5.3" , {min:6, error: true}) |
throws |
intType.castArray(undefined ) |
[] |
intType.castArray("5" ) |
[5] |
intType.castArray([5,null] ) |
[5] |
intType.castArray(["5",null] , {keepNullItems: true}) |
[5, null] |
intType.castMap(undefined ) |
{} |
intType.castMap({k1:5, k2: null} ) |
{k1: 5} |
intType.castMap({k1:{id:5}, k2: null} , {keepNullValues: true}) |
{k1: 5, k2: null} |
boolType
- Returns a boolean in any event ==> boolean|null
- Converts most-used text to boolean [ true, t, yes, y, on, false, f, no, n, off ]
- Converts positive numbers to true
- Converts negative and zero numbers to false
Specific Options
Name | Type | Default | cast | castArray | castMap | Info |
---|---|---|---|---|---|---|
def | boolean | null | [x] | [x] | [x] | if null use default value |
Most Used Function
- boolType.is(value: any): boolean
- boolType.cast(value: any, opt?: Options, req?: Request): boolean|null
- boolType.castArray(value: any, opt?: Options, req?: Request): Array<boolean> // or Array<boolean|null> with keepNullItems:true
- boolType.castMap(value: any, opt?: Options, req?: Request): Record<string, boolean> // or Record<string, boolean|null> with keepNullValues:true
Samples
Call | Result |
---|---|
boolType.cast(undefined ) |
null |
boolType.cast(5 ) |
true |
boolType.cast(-1 ) |
false |
boolType.cast("on" ) |
true |
boolType.cast("off" ) |
false |
boolType.cast(() => "t" ) |
true |
boolType.cast(["yes"] ) |
true |
boolType.cast(" foo bar " , {def: false}) |
false |
boolType.cast("foo" , {mandatory:true}) |
logs |
boolType.castArray(undefined ) |
[] |
boolType.castArray("5" ) |
[true] |
boolType.castArray(["on",null] ) |
[true] |
boolType.castArray(["off",null] , {keepNullItems: true}) |
[false, null] |
boolType.castMap(undefined ) |
{} |
boolType.castMap({k1:5, k2: null} ) |
{k1: true} |
boolType.castMap({k1:false, k2: null} , {keepNullValues: true}) |
{k1: false, k2: null} |
functionType
- Returns a function in any event ==> function|null
- Checks argument size if need
- Supports default function if null
Specific Options
Name | Type | Default | cast | castArray | castMap | Info |
---|---|---|---|---|---|---|
def | Function | null | [x] | [x] | [x] | if null use default value |
min | integer | null | [x] | [x] | [x] | problem if argument size is less than |
max | integer | null | [x] | [x] | [x] | problem if argument size is greater than |
Most Used Function
- functionType.is(value: any): boolean
- functionType.cast(value: any, opt?: Options, req?: Request): Function|null
- functionType.castArray(value: any, opt?: Options, req?: Request): Array<Function> // or Array<Function|null> with keepNullItems:true
- functionType.castMap(value: any, opt?: Options, req?: Request): Record<string, Function> // or Record<string, Function|null> with keepNullValues:true
Samples
Call | Result |
---|---|
functionType.cast(undefined ) |
null |
functionType.cast(undefined , {def: () => 'foo'}) |
() => 'foo' |
functionType.cast((p1) => 2 * p1 , {min: 2}) |
logs |
functionType.cast("foo" , {mandatory:true, error: true}) |
throws "foo" is not a function |
functionType.castArray(undefined ) |
[] |
functionType.castArray([() => 'foo',null] ) |
[() => 'foo'] |
functionType.castArray([() => 'foo',null] , {keepNullItems: true}) |
[() => 'foo', null] |
functionType.castMap(undefined ) |
{} |
functionType.castMap({k1:() => 'foo', k2: null} ) |
{k1: () => 'foo'} |
functionType.castMap({k1:() => 'foo', k2: null} , {keepNullValues: true}) |
{k1: () => 'foo', k2: null} |
Other Types
- @todo
-
dateType
Converts any date related (string, number, integer array, Date, Moment) value Date type
Date
Date[] | Array<Date>
{[key: string]: Date} | Record<string, Date>
-
isoDatetimeType [format:
yyyy-mm-ddTHH:ii:ss.eee.Z
]Converts any date related (string, number, integer array, Date, Moment) value ISO Datetime format
string
string[] | Array<string>
{[key: string]: string} | Record<string, string>
-
isoDateType [format:
yyyy-mm-dd
]Converts any date related (string, number, integer array, Date, Moment) value ISO Date format
string
string[] | Array<string>
{[key: string]: string} | Record<string, string>
-
isoTimeType [format:
HH:ii:ss.eee.Z
]Converts any date related (string, number, integer array, Date, Moment) value ISO Time format
string
string[] | Array<string>
{[key: string]: string} | Record<string, string>
-
momentType
Converts any date related (string, number, integer array, Date, Moment) value Moment class
Moment
Moment[] | Array<Moment>
{[key: string]: Moment} | Record<string, Moment>
-
regExpType
Validates Regexp (also converts string to RegExp)
RegExp
RegExp[] | Array<RegExp>
{[key: string]: RegExp} | Record<string, RegExp>
-
anyType
For anonymous scalar type usage
any
any[] | Array<any>
{[key: string]: any} | Record<string, any>
-
arrayType
For anonymous array usage
any[] | Array<any>
any[][] | Array<Array<any>>
{[key: string]: any[]} | Record<string, Array<any>>
-
objectType
For anonymous object (also be called as map or record) usage
{[key: string]: any} | Record<string, any>
{[key: string]: any}[] | Array<Record<string, any>>
{[key: string]: {[sub: string]: any}} | Record<string, Record<string, any>>