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

@wareset-utilites/typed

v0.4.0

Published

WIP: Checking and comparing with the prototype

Downloads

11

Readme

@wareset-utilites/typed

He can return the constructor, return the entire chain of constructors, check the availability of constructors.

Attention:

<=0.1.1

Version <=0.1.1 returns the prototypes and not the constructors!

>=0.3.0

Starting with the >=0.3.0 version, returns the prototype and the constructor (if object has been created like Object.create({ q: 1 }), returns prototype { q: 1 })!.

Installation

npm i @wareset-utilites/typed ## yarn add @wareset-utilites/typed

Usage

Import:

import typed from '@wareset-utilites/typed'

Returns prototype:

// they do not have prototypes
expect(typed(undefined)).toBe(undefined)
expect(typed.of(undefined)).toEqual([])

expect(typed(null)).toBe(undefined)
expect(typed.of(null)).toEqual([])
// but:
expect(typed(Object.create(null))).toBe(null)
expect(typed.of(Object.create(null))).toEqual([null])

const boolean = true
expect(typed(boolean)).toBe(Boolean)
expect(typed.of(boolean)).toEqual([Boolean, Object])

const number = 304010
expect(typed(number)).toBe(Number)
expect(typed.of(number)).toEqual([Number, Object])

const dataView = new DataView(new ArrayBuffer(16))
expect(typed(dataView)).toBe(DataView)
expect(typed.of(dataView)).toEqual([DataView, Object])

expect(typed(() => {})).toBe(Function)
expect(typed.of(() => {})).toEqual([Function, Object])

const AsyncFunction = (async () => {}).constructor
expect(typed(async () => {})).toBe(AsyncFunction)
expect(typed.of(async () => {})).toEqual([AsyncFunction, Function, Object])
// etc...

test('Class:', () => {
  class Qwer {}
  class Wert extends Qwer {}
  class Erty extends Wert {}

  const newClass = new Erty()
  expect(typed(newClass)).toBe(Erty)
  expect(typed.of(newClass)).toEqual([Erty, Wert, Qwer, Object])
})

test('DIV:', () => {
  const div = document.createElement('div')
  expect(typed(div)).toBe(HTMLDivElement)
  expect(typed.of(div)).toEqual([
    HTMLDivElement,
    HTMLElement,
    Element,
    Node,
    EventTarget,
    Object
  ])
})
// etc...

// Object:
const object = Object.create(null)
expect(typed(object)).toBe(null)
expect(typed.of(object)).toEqual([null])

const proto = { q: 1 }
const object = Object.create(proto)
expect(typed(object)).toBe(proto)
expect(typed.of(object)).toEqual([{ q: 1 }, Object])

const object2 = Object.create(object)
expect(typed(object2)).toEqual({})
expect(typed.of(object2)).toEqual([{}, { q: 1 }, Object])

const proto2 = [1, 2, 3]
const object3 = Object.create(proto2)
expect(typed(object3)).toBe(proto2)
expect(typed.of(object3)).toEqual([[1, 2, 3], Array, Object])

const object4 = Object.create(object3)
expect(typed(object4)).toBe(object3)
expect(typed.of(object4)).toEqual([{}, [1, 2, 3], Array, Object])

Check the availability of prototypes:

expect(typed(Infinity, Number)).toBe(true) // Number
expect(typed(Infinity, String)).toBe(false)
expect(typed(Infinity, Number, String)).toBe(true) // Number
expect(typed(Infinity, Array, String)).toBe(false)

expect(typed.of(Infinity, Array, String)).toBe(false)
expect(typed.of(Infinity, Number, Array, String)).toBe(true)
expect(typed.of(Infinity, Object, Array, String)).toBe(true) // Object

Methods: typed.try(value, ...types) and typed.of.try(value, ...types)

Check the availability of prototypes:

console.log(typed.try(123)) // RETURN: 123
console.log(typed.of.try(123)) // RETURN: 123

console.log(typed.try(123, Number)) // RETURN: 123
console.log(typed.of.try(123, [String, Object])) // RETURN: 123

console.log(typed.try(123, String)) // throw { value: 123, types: [String] }
console.log(typed.of.try(123, [String]))//throw { value: 123, types: [String] }

License

MIT