npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

checkpointjs

v0.0.9

Published

Validate and transform data

Downloads

10

Readme

THIS LIBRARY IS CURRENTLY UNSTABLE! USE AT YOUR OWN RISK!

Checkpoint.js

Validate and transform data.

Setup

Download

Install the checkpointjs package with a package manager like npm or Yarn.

You can also download and extract a release from here from the Checkpoint.js GitHub repository releases page.

Using the Library

The library can be used in two different ways:

Direct Function Import

import { validate } from 'checkpointjs'

const data = {
  foo: 'bar',
  123: 456
}

const result = validate(data, {
  schema: {
    foo: { isRequired: true, type: 'string' },
    123: { type: 'number' }
  },
  type: 'object'
})

Checkpoint Instantiation

import checkpoint from 'checkpointjs'

const data = {
  foo: 'bar',
  123: 456
}

const result = checkpoint(data).validate({
  schema: {
    foo: { isRequired: true, type: 'string' },
    123: { type: 'number' }
  },
  type: 'object'
})

Note: This library supports TypeScript. The source is completely written in it. Declaration files are included in the dist/ folder.

API

Validate

Validates the input data. Returns the results of the validation.

Functions

// Direct Function Import
validate(data, rules)

// Checkpoint Instantiation
checkpoint(data).validate(rules)

Result

TODO

Schema

allowNull
  • Description: Determines if a null value is allowed.
  • Type: boolean
  • Default: false
// Primitive
const primitiveData = null
const primitiveValidationResult = validate(primitiveData, {
  schema: { allowNull: true },
  type: 'primitive'
})
console.log(primitiveValidationResult.pass) // true

// Object
const objectData = { foo: null }
const objectValidationResult = validate(objectData, {
  schema: {
    foo: { allowNull: true }
  },
  type: 'object'
})
console.log(objectValidationResult.pass) // true

// Array of primitives
const primitiveArrayData = [null, null]
const primitiveArrayValidationResult = validate(primitiveArrayData, {
  schema: { allowNull: true },
  type: 'array',
  arrayType: 'primitive'
})
console.log(primitiveArrayValidationResult.pass) // true

// Array of objects
const objectArrayData = [
  { foo: null },
  { foo: null }
]
const objectArrayValidationResult = validate(objectArrayData, {
  schema: { allowNull: true },
  type: 'array',
  arrayType: 'object'
})
console.log(objectArrayValidationResult.pass) // true
isRequired
  • Description: Determines if the value is required.
  • Type: boolean
  • Default: false
// Primitive
const primitiveData = 123
const primitiveValidationResult = validate(primitiveData, {
  schema: { isRequired: true },
  type: 'primitive'
})
console.log(primitiveValidationResult.pass) // true

// Object
const objectData = { foo: 123 }
const objectValidationResult = validate(objectData, {
  schema: {
    foo: { isRequired: true }
  },
  type: 'object'
})
console.log(objectValidationResult.pass) // true

// Array of primitives
const primitiveArrayData = [123, 456]
const primitiveArrayValidationResult = validate(primitiveArrayData, {
  schema: { isRequired: true },
  type: 'array',
  arrayType: 'primitive'
})
console.log(primitiveArrayValidationResult.pass) // true

// Array of objects
const objectArrayData = [
  { foo: 123 },
  { foo: 456 }
]
const objectArrayValidationResult = validate(objectArrayData, {
  schema: { isRequired: true },
  type: 'array',
  arrayType: 'object'
})
console.log(objectArrayValidationResult.pass) // true
stringValidation

TODO

type
  • Description: Requires a matching type for the value.
  • Type: string
// Primitive
const primitiveData = 'foo'
const primitiveValidationResult = validate(primitiveData, {
  schema: { type: 'string' },
  type: 'primitive'
})
console.log(primitiveValidationResult.pass) // true

// Object
const objectData = { foo: 123 }
const objectValidationResult = validate(objectData, {
  schema: {
    foo: { type: 'number' }
  },
  type: 'object'
})
console.log(objectValidationResult.pass) // true

// Array of primitives
const primitiveArrayData = [true, false]
const primitiveArrayValidationResult = validate(primitiveArrayData, {
  schema: { type: 'boolean' },
  type: 'array',
  arrayType: 'primitive'
})
console.log(primitiveArrayValidationResult.pass) // true

// Array of objects
const objectArrayData = [
  { foo: 'bar' },
  { foo: 'baz' }
]
const objectArrayValidationResult = validate(objectArrayData, {
  schema: { type: 'string' },
  type: 'array',
  arrayType: 'object'
})
console.log(objectArrayValidationResult.pass) // true

Options

exitASAP

TODO

requireMode

TODO

Transform

Transforms and mutates the input data. Returns the transformed data.

Functions

// Direct Function Import
transform(data, commands)

// Checkpoint Instantiation
checkpoint(data).transform(commands)

Commands

clean
  • Description: Removes undefined values.
// Object
const objectData = { a: 123, b: undefined, c: 456, d: 789, e: undefined }
transform(objectData, 'clean')
console.log(objectData) // { a: 123, c: 456, d: 789 }
replace
  • Description: Replaces values with another value.
// Object
const objectData = { a: 123, b: 456, c: 789 }
transform(objectData, { name: 'replace', options: [456, 'xyz'] })
console.log(objectData) // { a: 123, c: 'xyz', d: 789 }
trim
  • Description: Trims whitespace from strings.
// Object
const objectData = { a: 'hey    ', b: '    ho', c: '     let\'s go     ' }
transform(objectData, 'trim')
console.log(objectData) // { a: 'hey', c: 'ho', d: 'let\'s go' }

Checkpoint

TODO

License

This open source project is licensed under the MIT License.