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

typelike

v1.0.4

Published

Deep type checking by comparison

Downloads

154

Readme

Actions Status Actions Status

typelike

With typelike you can reliably determine if an object resembles other template object(s) used as reference. The comparison is done using the keys and the type of data where applicable.

Examples

Example 1:

import { typelike } from 'typelike'

const testObject = {
  lvl1: { lvl2: [1, 2, 3], sm: 'type ... like' },
  arr: [[1, 'xyz'], 'abcdef']
}

const templateObject = {
  lvl1: { lvl2: [3, 4, 212], sm: '' },
  arr: [[44, ''], '']
}

console.log(typelike(testObject, templateObject)) // true

Example 2:

import { typelike } from 'typelike'

function contentGenerator () {
  // ...
  return {
    lvl1: {
      lvl2: {
        lvl3: {
          lvl4: {
            item1: [1, 2, 3],
            item2: true
          }
        }
      }
    },
    arr: [[1, 'xyz'], 'abcdef'],
    basic: 'test'
  }
}

const testObject = contentGenerator()

const templateObject = {
  lvl1: {
    lvl2: {
      lvl3: {
        lvl4: {
          item1: [44, 66, 88],
          item2: false
        }
      }
    }
  },
  arr: [[45, 'sample'], String('string')],
  basic: 'test'
}

console.log(typelike(testObject, templateObject)) // true

How to compare with typelike

typelike iterates as deeply as possible any arrays, objects, maps and sets, while taking into account typelike Settings. For type checking typelike uses xtypeof which is included as part of the application.

In order to pass typelike tests the following logic applies:

  • for arrays, objects and maps tests pass if:
    • keys are identical for both subject object and template object and ...
    • the type of data corresponding to a key is identical for both subject object and template object.
    • if the data corresponding to a key is iterable as understood by typelike (array, object, map, set) the checks continue at the next deeper level
  • for sets tests pass if:
    • the type of data corresponding to an entry is identical for both subject set and template set.
    • if the data corresponding to an entry is iterable as understood by typelike (array, object, map, set) the checks continue at the next deeper level
  • for any other type tests pass if:
    • the type of subject data is identical for both subject object and template object

Using multiple templates with typelike

Multiple templates can be used with every call to typelike. If the subject object (which should be the first parameter specified) matches any of them then typelike will return true. Example:

import { typelike } from 'typelike'

const testObject = {
  lvl1: { lvl2: [1, 2, 3], sm: 'type ... like' },
  arr: [[1, 'xyz'], 'abcdef']
}

const template1 = {
  lvl1: {
    lvl2: {
      lvl3: {
        lvl4: {
          item1: [1, 2, 3],
          item2: true
        }
      }
    }
  },
  arr: [[1, 'xyz'], 'abcdef'],
  basic: 'test'
}
const template2 = {
  lvl1: { sm: 'type ... like' },
  arr: [[1], 'abcdef']
}
const template3 = {
  lvl1: { lvl2: [3, 4, 212], sm: '' },
  arr: [[44, ''], '']
}

console.log(typelike(testObject, template1, template2)) // false
console.log(typelike(testObject, template1, template2, template3)) // true

typelike Settings

typelike behavior can be changed with a couple of settings. In order to run with altered the settings, a special function typelikeCustom is exported. The settings are valid for a single call to typelikeCustom. Subsequent calls to typelike will use the default settings as listed below.

typelikeCustom behaves in the same way as typelike, but expects that the last parameter passed to be a special setting object which takes the following default format/values:

{
  maxDepth: 0,
  properties: {
    allowMissing: false,
    allowNull: false
  }
}
  • maxDepth indicates the maximum depth allowed for iterations. Defaults to 0 (unlimited depth levels)
  • properties.allowMissing takes a boolean value and indicates that the keys/properties are mandatory (false), or not mandatory and can miss from arrays, objects, maps and sets (true). Defaults to false (all keys/properties are mandatory)
  • properties.allowNull takes a boolean value and indicates that null value keys/properties of a target object, match any corresponding types in the template object(s) (true). Defaults to false which means that null value keys/properties of a target object match corresponding null value keys/properties of template object(s).

Example:

import { typelikeCustom } from 'typelike'

const testObject = {
  lvl1: { sm: 'type ... like' },
  arr: [[1], 'abcdef']
}

const templateObject = {
  lvl1: { lvl2: [3, 4, 212], sm: '' },
  arr: [[44, ''], '']
}

const settings = {
  maxDepth: 3,
  properties: {
    allowMissing: true
  }
}

console.log(typelikeCustom(testObject, templateObject, settings)) // true

Others

typelike is © Copyright 2020-2024 Nicolae Iotu, [email protected]