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

serialized

v1.0.0

Published

Serialize javascript objects into compact strings

Downloads

8

Readme

serialized

Serialize and deserialize javascript objects into compact, url encoded strings. Useful when you have to store rich objects (such as ElasticSearch queries) in browser url.

Example

// Describe field types
const ProductField = oneOfType([
  constant('id'),
  constant('name'),
  constant('price'),
  constant('category'),
  constant('manufacturer')
])

const PriceRange = mapOf(
  constant('price'),
  objectOf({ gte: numberType(), lte: numberType() })
)

function getType(productField) {
  switch (productField) {
    case 'id': return integerType()
    case 'price': return numberType()
    default: return stringType()
  }
}

const TermOrWildcard = withCalculatedType(
  getType,
  (from, Calculated) => mapOf(from(ProductField), Calculated)
)

const boolType = template(T => oneOfType([
  objectOf({ should: arrayOfType(T), must: arrayOfType(T) }),
  objectOf({ should: arrayOfType(T) }),
  objectOf({ must: arrayOfType(T) })
]))

const Query = oneOfType([
  objectOf({ term: TermOrWildcard }),
  objectOf({ wildcard: TermOrWildcard }),
  objectOf({ range: PriceRange }),
  objectOf({ bool: boolType(() => Query) })
])

const Search = objectOf({
  from: integerType(),
  size: oneOfType([constant(10), constant(100), constant(1000)]),
  query: Query
})

// Complex query
const search = {
  from: 15,
  size: 100,
  query: {
    bool: {
      must: [{
        bool: {
          should: [
            { wildcard: { name: '*tablet*' }},
            { term: { category: 'Electronics/Tablets' }}]}}, {
          bool: {
            should: [{
              bool: {
                must: [
                  { term: { manufacturer: 'Apple' }},
                  { range: { price: { gte: 0, lte: 1000 }}}]}}, {
              bool: {
                must: [
                  { term: { manufacturer: 'Samsung' }},
                  { range: { price: { gte: 0, lte: 500 }}}]}}]}}]}}}

// Serialize object
const serializedString = Search.serialize(search)
//=> f$1323111*tablet*$$03Electronics%2FTablets$$$313204Apple$$20$1000$$$3204Samsung$$20$500

Search.deserialize(serializedString)
//=> equals to 'search'

API

stringType([length])

Simple string. By default serializes strings with encodeURIComponent

  • length Accept only strings with provided length

integerType([max])

Positive integer

  • max Accept only integers between 0 and max

numberType()

Simple number

constant(value)

  • value Any javascript primitive type

objectOf(schema)

  • schema Object with token type values
const Person = objectOf({
  name: stringType(),
  age: integerType(),
})

mapOf(keyToken, valueToken)

Javascript object as map

  • keyToken Type of map keys
  • valueToken Type of map values

oneOfType(tokens)

Union type

  • tokens Array of possible types
const OptionalString = oneOfType([
  constant(undefined),
  constant(null),
  stringType(),
])

arrayOfType(token, [length])

  • token Type of array item
  • length Accept only arrays with provided length
const Matrix = arrayOfType(numberType(), 9)

template(callback)

const nullableType = template(T => oneOfType([
  constant(null),
  T,
]))
const NullableString = nullableType(stringType())

withCalculatedType(getType, callback)

Utility for deferred type calculation