
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@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
The npm package @betafcc/dataclass receives a total of 6 weekly downloads. As such, @betafcc/dataclass popularity was classified as not popular.
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
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.