2.0.45 • Published 3 years ago

@huzapi/scalar-type v2.0.45

Weekly downloads
136
License
ISC
Repository
gitlab
Last release
3 years ago

#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

NameTypeDefaultcastcastArraycastMapInfo
logbooleantruexxxlogs when a problem
errorbooleanfalsexxxthrows when a problem
mandatorybooleanfalsexooproblem if null
uniquebooleanfalseoxoremoves duplicated items in array
keepNullItemsbooleanfalseoxokeeps null items in array (default: remove nulls)
minItemsintegernulloxoproblem if array size is less than
maxItemsintegernulloxoproblem if array size is greater than
keepNullValuesbooleanfalseooxkeeps null values in map (default: remove nulls)
minKeysintegernullooxproblem if object key size is less than
maxKeysintegernullooxproblem 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

NameTypeDefaultcastcastArraycastMapInfo
noTrimbooleanfalsexxxignore trim (except uuidType)
minLengthintegernullxxxproblem if text length is less than (except uuidType)
maxLengthintegernullxxxproblem if text length is greater than (except uuidType)
patternstringnullxxxfor 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

CallResult
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

NameTypeDefaultcastcastArraycastMapInfo
defintegernullxxxif null use default value
minintegernullxxxproblem if value is less than
maxintegernullxxxproblem 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

CallResult
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

Specific Options

NameTypeDefaultcastcastArraycastMapInfo
defbooleannullxxxif 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

CallResult
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

NameTypeDefaultcastcastArraycastMapInfo
defFunctionnullxxxif null use default value
minintegernullxxxproblem if argument size is less than
maxintegernullxxxproblem 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

CallResult
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>>
2.0.45

3 years ago

2.0.44

3 years ago

2.0.43

3 years ago

2.0.41

3 years ago

2.0.40

3 years ago

2.0.39

3 years ago

2.0.38

3 years ago

2.0.37

3 years ago

2.0.35

3 years ago

2.0.36

3 years ago

2.0.34

3 years ago

2.0.33

3 years ago

2.0.31

3 years ago

2.0.32

3 years ago

2.0.30

3 years ago

2.0.29

3 years ago

2.0.28

3 years ago

2.0.27

3 years ago

2.0.26

3 years ago

2.0.25

3 years ago

2.0.24

3 years ago

2.0.23

3 years ago

2.0.22

3 years ago

2.0.21

3 years ago

2.0.19

3 years ago

2.0.20

3 years ago

2.0.18

3 years ago

2.0.17

3 years ago

2.0.16

3 years ago

2.0.15

3 years ago

2.0.14

3 years ago

2.0.13

3 years ago

2.0.12

3 years ago

2.0.11

3 years ago

2.0.10

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.1

3 years ago