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

datacasting

v0.0.1

Published

Data converting by schema

Downloads

5

Readme

🗄️ Datacasting

coverage statements coverage lines coverage functions coverage branches

Make schemas and cast data declaratively

Installing

npm install --save datacasting
# or
yarn add datacasting

Basic usage

Working with an array of objects

import { scheme, toDate } from 'datacasting'

const userScheme = scheme({
  verified: Boolean,
  created_at: toDate('yyyy-MM-dd'),
})

const casted = userScheme.cast([
  {
    first_name: 'Jonh',
    last_name: 'Doe',
    verified: 0,
    created_at: '2020-01-23',
  }
])

console.log(casted)
// [
//   {
//     "first_name": "Jonh",
//     "last_name": "Doe",
//     "verified": false,
//     "created_at": Date(2020, 0, 23)
//   }
// ]

const rewrited = userScheme.rewrite([
  {
    first_name: 'Jonh',
    last_name: 'Doe',
    verified: 0,
    created_at: '2020-01-23',
  }
])

console.log(rewrited)
// [
//   {
//     "verified": false,
//     "created_at": Date(2020, 0, 23)
//   }
// ]

Working with a single object

import { scheme, toDate } from 'datacasting'

const userScheme = scheme({
  verified: Boolean,
  created_at: toDate('yyyy-MM-dd'),
})

const casted = userScheme.cast({
  first_name: 'Jonh',
  last_name: 'Doe',
  verified: 0,
  created_at: '2020-01-23',
})

console.log(casted)
// {
//  "first_name": "Jonh",
//  "last_name": "Doe",
//  "verified": false,
//  "created_at": Date(2020, 0, 23)
// }

const rewrited = userScheme.rewrite({
  first_name: 'Jonh',
  last_name: 'Doe',
  verified: 0,
  created_at: '2020-01-23',
})

console.log(rewrited)
// {
//  "verified": false,
//  "created_at": Date(2020, 0, 23)
// }

API

Scheme methods

  • cast – converts data according to the schema, inheriting the passed fields, even if they were not declared in the schema.
  • rewrite - converts data according to the schema without inheriting the original fields. The result will be an object only from the fields declared in the schema.

Available caster functions

  • First, you can use the built-in type conversion: String, Number and Boolean

    import { scheme } from 'datacasting'
    
    const dataScheme = scheme({
      user_id: String,
      verified: Boolean,
      role: Number,
    })
  • replace(searchValue: string | RegExp, replaceValue: string | RegExp): (value: string) => string

    import { scheme, replace } from 'datacasting'
    
    const dataScheme = scheme({
      name: replace('admin', 'nobody'),
    })
    
    const casted = dataScheme.cast({
      name: "John Doe - admin",
    })
    
    console.log(casted)
    // {
    //   "name": "John Doe - nobody"
    // }
  • toArrayOf<K = StringConstructor | NumberConstructor | BooleanConstructor>(constructor: K): (value: any) => K[]

    import { scheme, toArrayOf } from 'datacasting'
    
    const dataScheme = scheme({
      skill: toArrayOf(String),
    })
    
    console.log(dataScheme.cast({
      skill: "javascript",
    }))
    // {
    //   "skill": ["javascript"]
    // }
    
    console.log(dataScheme.cast({
      skill: ["javascript", "vue", "react"],
    }))
    // {
    //   "skill": ["javascript", "vue", "react"]
    // }
  • toDate(fromFormat: string): (value: string) => Date

    Argument fromFormat refers to date-fns tokens. See: date-fns parse tokens.

    import { scheme, toDate } from 'datacasting'
    
    const dataScheme = scheme({
      created_at: toDate('yyyy-MM-dd'),
    })
    
    console.log(dataScheme.cast({
      created_at: "2020-01-15",
    }))
    // {
    //   "created_at": Date(2020, 0, 15)
    // }
  • toInteger(value: any): number

    import { scheme, toInteger } from 'datacasting'
    
    const dataScheme = scheme({
      balance: toInteger,
    })
    
    console.log(dataScheme.cast({
      balance: '150.33',
    }))
    // {
    //   "balance": 150
    // }
  • toLowerCase(value: any): string

    import { scheme, toLowerCase } from 'datacasting'
    
    const dataScheme = scheme({
      name: toLowerCase,
    })
    
    console.log(dataScheme.cast({
      name: 'John Doe',
    }))
    // {
    //   "name": "john doe"
    // }
  • toUpperCase(value: any): string

    import { scheme, toUpperCase } from 'datacasting'
    
    const dataScheme = scheme({
      name: toUpperCase,
    })
    
    console.log(dataScheme.cast({
      name: 'John Doe',
    }))
    // {
    //   "name": "JOHN DOE"
    // }

Write your own caster function

Arguments:

  • {any} value
  • {string} key
  • {Object} data

Returns:

  • {any} result
import { scheme } from 'datacasting'

const dataScheme = scheme({
  role_id: (value, key, data) => {
    return data.role.id
  })
})

const casted = dataScheme.cast({
  name: 'Jonh Doe',
  role: {
    id: 1,
    name: 'user',
  }
})

console.log(casted)
// {
//  "name": "John Doe",
//  "role_id": 1,
//  "role": {
//    "id": 1,
//    "name": "user"
//  },
// }

License

MIT © Daniel Slepov