⭐ Master your input data like a pro! ⭐

✔️ Node.js & browser ✔️ Only 1kb! ✔️ Fully typed ✔️ No dependencies

Simple helper library which allows you to ensure the integrity and type safety of your app’s input data.

It works by exposing a number of functions which check wether an unknown value is either a string, boolean, float, integer or object. You can also turn strings into numbers or booleans.

🚀 Getting started

npm

$ npm i primitive-parser

yarn

$ yarn add primitive-parser

💡 Examples

🕵 Make sure nasty user inputs do not reach your app.

// ...

import express from 'express'
import { getPositiveInteger } from 'primitive-parser'

// ...

app.get('/:user/:id', function (req, res) {
  const id = getPositiveInteger(req.params.id)
  if (!id) {
    return res.status(400)
  }

  // At this point, "id" is infered as "number".
  showUser(req, res, id)
})

// ...

🧛 Unsure about external events? No more.

import { getObject, getString } from 'primitive-parser'
import { useState } from 'react'
import SketchyInput from 'unknown-source'

const SafeInput = () => {
  const [value, setValue] = useState<string>('')

  const onValueChange = (sketchyEvent: any) => {
    const safeValue = getString(getObject(sketchyEvent)?.value)

    // Important over "!safeValue" since the latter returns "false" on valid empty strings.
    if (safeValue !== undefined) {
      // "safeValue" is inferred as "string" inside this block scope.
      setValue(safeValue)
    } else {
      // Error handling
    }
  }

  return <SketchyInput onValueChange={onValueChange} value={value} />
}

🍺 Full helper list

Check out the tests file for a full behavioural overview.

String

const getString = (value: unknown): string | undefined

getString('foo') // "foo"
getObject({ foo: 'bar' }) // undefined

Boolean

const getBoolean = (value: unknown): boolean | undefined

getBoolean('true') // undefined
getBoolean(true) // true

Boolean from String

const getBooleanFromString = (value: unknown): boolean | undefined

getBoolean('true') // true
getBoolean(true) // undefined

Float

const getFloat = (value: unknown): number | undefined

getFloat('12.34') // undefined
getFloat(12.34) // 12.34

Float from String

const getFloatFromString = (value: unknown): number | undefined

getFloat('12.34') // 12.34
getFloat(Infinity) // undefined
getFloat(12.34) // undefined

Integer

const getInteger = (value: unknown): number | undefined

getInteger('1234') // undefined
getInteger(12.34) // undefined
getFloat(1234) // 1234

Integer from string

const getIntegerFromString = (value: unknown): number | undefined

getIntegerFromString('1234') // 1234
getIntegerFromString(1234)) // undefined
getIntegerFromString(12.34) // undefined

Positive Integer

const getPositiveInteger = (value: unknown): number | undefined

getPositiveInteger('1') // undefined
getPositiveInteger(0) // undefined
getPositiveInteger(-1) // undefined
getPositiveInteger(1) // 1

Positive Integer from String

const getPositiveInteger = (value: unknown): number | undefined

getPositiveInteger('1') // undefined
getPositiveInteger(1) // 1
getPositiveInteger(-1) // undefined

Object (general)

const getObject = (value: unknown): object | undefined

getObject('foo')) // undefined
getObject({ foo: 'bar' }) // { foo: "bar" }

GitHub

View Github