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

@betafcc/dataclass

v0.2.0

Published

Type-safe dataclass inspired by python 'dataclasses' stdlib

Downloads

3

Readme

@betafcc/dataclass

Small 'dataclass' utility that allows type-safe mixins and mutation-free transformations

install

npm i @betafcc/dataclass

Usage

Define fields in Dataclass<{...props}> and extend it:

import {Dataclass} from '@betafcc/dataclass'

class Point extends Dataclass<{x: number, y: number}> { }

The constructor with the typed signature will be created for you:

Use pluck to read fields:

import {pluck} from '@betafcc/dataclass'

const p = new Point({x: 100, y: 200})

const {x} = pluck(p) // x = 100
const y = pluck(p, 'y') // y = 200
const xy = pluck(p, ['x', 'y']) // xy = [100, 200]

And replace to 'change' them (actually, create new objects with the modifications)

import {replace} from '@betafcc/dataclass'

console.log(replace(p, {x: 200})) // Point({x: 200, y: 200})
console.log(p) // still Point({x: 100, y: 200})

// can use with a function from current fields:
console.log(replace(p, old => ({
  x: old.x + old.y
}))) // Point({x: 300, y: 200})

More complete example, showing the use of mixins:

import {Dataclass, mixin, replace, pluck} from '@betafcc/dataclass'

class Point extends Dataclass<{x: number, y: number}> {
  // use the fake `this` parameter if you want to update
  // type info on subclasses
  move<A extends this>(this: A, dx: number, dy: number) {
    const {x, y} = pluck(this)
    return replace(this, {x: x + dx, y: y + dy})
  }
}


class Rectangle extends Dataclass<{width: number, height: number}> {
  scale<A extends this>(this: A, dw: number, dh: number) {
    return replace(this, ({width, height}) => ({
      width: width + dw,
      height: height + dh
    }))
  }
}

// Apply previous classes as mixin, the result is a Dataclass itself (also can be used as mixin),
// with the fields of the the base dataclasses merged and methods inherithed
class Geometry extends mixin(Point, Rectangle) {
  moveAndScale(dx: number, dy: number, dw: number, dh: number) {
    return this.move(dx, dy).scale(dw, dh)
  }
}

The type system will detect the new fields:

And inherited methods will know which class they belong now: