Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
A minimalistic class system designed for flexibility, functional programming. Inspired from Golang's Struct concept.
Motivation:
new-struct
is small and simple. All it does is composing functions and objects.new
and this
keywords. So, you'll never have to fix scopes.new-struct
and structs.How does a struct look?
var Struct = require('new-struct')
var Animal = Struct({
sleep: sleep,
speak: speak
})
module.exports = Animal;
function sleep (animal) { console.log('zzzz'); }
function speak (animal, text) {
console.log('%s says %s', animal.name, text);
}
Check out Usage for more info about it.
$ npm install new-struct
A new struct is defined by an object of methods:
Struct = require('new-struct')
Animal = Struct({
sleep: sleep,
run: run,
speak: speak
})
function sleep (animal) {
console.log('zzz');
}
function speak (animal, sound) {
console.log('%s says %s', animal.name, sound)
}
function run (animal) {
console.log('%s is running', animal.name)
}
To create a an instance of the Animal
struct, just call it with an object.
this
and new
keywords are not needed, everything is just functions.
dongdong = Animal({ name: 'dongdong' })
blackbear = Animal({ name: 'blackbear' })
dongdong.name
// => 'dong dong'
dongdong.run()
// dongdong is running
blackbear.sleep()
// blackbear is sleeping
It doesn't support constructors, but constructor-like factory functions are easy to implement:
function NewAnimal (name, age) {
return Animal({ name: name, age: age })
}
Note that you can attach your constructor as a static method. So, you could have such a module:
Animal = Struct({
New: New,
run: run,
speak: speak
})
module.exports = Animal;
function New (name, age) {
return Animal({ name: name, age: age })
}
function speak (animal, sound) {
console.log('%s says %s', animal.name, sound)
}
function run (animal) {
console.log('%s is running', animal.name)
}
This will allow other modules requiring this have more flexibility:
Animal = require('./animal')
// You can either create using constructor:
Animal.New('dong dong', 13)
// Or calling the constructor itself:
Animal({ name: 'dong dong', age: 13 })
// You can also access the methods of Animal:
Animal.methods.run({ name: 'black bear' })
// will output: black bear is running
You can create structs that mixes other ones:
Animal = require('./animal')
Cat = Struct(Animal, {
meow: meow
})
function meow (cat) {
Animal.methods.speak(cat, 'meooww')
}
Notice that each struct has a property called methods
that keeps all the functions passed to it, including the ones derived from other structs.
See the tests and examples for more info, or create issues & send pull requests to improve the documentation.
FAQs
This package name is not currently in use, but was formerly occupied by a popular package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it.
The npm package new-struct receives a total of 2 weekly downloads. As such, new-struct popularity was classified as not popular.
We found that new-struct 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.