Options
All
  • Public
  • Public/Protected
  • All
Menu

@ezy/coerce-middleware

Index

Functions

coerce

  • coerce(property: string, coercePrimitive?: function): (Anonymous function)
  • Generate a connect-style middleware that coerce values for a req property

    Example

    import { coerce } from '@ezy/coerce-middleware'
    import express from 'express'
    const app = express()
    
    app.use(coerce('query'))
    
    app.get('/', (req, res) => {
      res.send(JSON.stringify(req.query))
    })
    
    app.listen(3000)
    
    // GET /?id=30&is_admin=true&name=awesome%20title
    // => {
    //   name: 'awesome title',
    //   id: 30,
    //   is_admin: true
    // }

    Parameters

    • property: string

      req property to coerce (eg: 'query', 'params', etc...)

    • Default value coercePrimitive: function = defaultCoercePrimitive

      an alternative function to coerce stringified primitives

        • (value: string): string | number | boolean | void
        • Parameters

          • value: string

          Returns string | number | boolean | void

    Returns (Anonymous function)

    a connect-style middleware

deepMap

  • deepMap(object: any, mapFn: function): any
  • a map-like function for primitives in nested object or array

    Parameters

    • object: any

      any object/array.

    • mapFn: function

      function to apply on primitive values.

        • (value: any): any
        • Parameters

          • value: any

          Returns any

    Returns any

    the mapped object/array.

defaultCoercePrimitive

  • defaultCoercePrimitive(value: string): string | number | false | true
  • coerce a stringified primitive value

    Parameters

    • value: string

      any stringified primitive value.

    Returns string | number | false | true

    the same value type converted.

Const isDecimal

  • isDecimal(value: string): boolean
  • check if a given value is actually a decimal number

    Parameters

    • value: string

    Returns boolean

Const isInt

  • isInt(value: string): boolean
  • check if a given value is actually an integer

    Parameters

    • value: string

    Returns boolean

isPrimitive

  • isPrimitive(value: any): boolean
  • check if a given value is a javascript primitive

    Parameters

    • value: any

    Returns boolean

Generated using TypeDoc