
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
@betafcc/dataclass
Advanced tools
Small 'dataclass' utility that allows type-safe mixins and mutation-free transformations
npm i @betafcc/dataclass
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:
FAQs
Type-safe dataclass inspired by python 'dataclasses' stdlib
We found that @betafcc/dataclass demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.