
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
A simple but powerful utility function, inspired by Kotlin's when
.
npm install wenn.js --save
const value = "Foo";
const result = wenn(value,
Case("Foo").Then(0),
Case("Bar").Then(1)
);
// result == 0
const value = "Foo";
wenn(value,
Case("Foo").Then(() => console.log("Value is 'Foo'")),
Case("Bar").Then(() => console.log("Value is 'Bar'"))
);
const value = "Test";
const result = wenn(value,
Case("Foo").Then(0),
Case("Bar").Then(1),
Else(-1)
);
// result == -1
If an Else case would be required but not found, there will be an error. You can always add
Else(undefined)
.
const value = "Test";
const result = wenn(value,
Case("Foo").Then(0),
Case("Bar").Then(1)
);
// ERROR: No case matched, but also no ELSE case given. You can add Else(undefined) to your cases to prevent an error.
Infer the types to prevent errors while compiling.
const value: string = "Test";
const result: string = wenn(value,
Case("Foo").Then("A"),
Case("Bar").Then("B"),
Else("?"));
wennChain
usagewennChain
allows you to propagate a value through all cases, until it doesn't match anymore or there's a Break()
.
const value = 4;
const result = wennChain(value,
Case(isNegative).Then(0),
Break(),
Case(isPositive).Then(x => x + 1),
Case(always).Then(x => x * 4),
Break(),
Case(always).Then(x => x * 3)
);
// result === 20
wennElvis
usagewennElvis
builds on top of wennChain
. It's basically chained isntUndefined
cases, with your given thens.
It allows a string with dot notation or even function calls, to access nested properties.
If a property wasn't found or a function call returns undefined, the function will safely return undefined.
const value = {
data: {
persons: [
{
name: "A",
age: 18
},
{
name: "B",
age: 21
},
{
name: "C",
age: 46
}
]
}
};
const result = wennElvis(value,
"data.persons",
arr => arr.find(v => v.age > 100),
"name"
);
// result === undefined, and no error
You can look at some examples in the test cases.
when
else
caseKotlin's when
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
wenn.js
wenn(x,
Case(1).Then(() => console.log("x == 1")),
Case(2).Then(() => console.log("x == 2")),
Else(() => console.log("x is neither 1 nor 2"))
);
Kotlin's when
when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}
wenn.js
wenn(x,
Case(0, 1).Then(() => console.log("x == 0 or x == 1")),
Else(() => console.log("otherwise"))
);
Kotlin's when
when (x) {
parseInt(s) -> print("s encodes x")
else -> print("s does not encode x")
}
wenn.js
wenn(x,
Case(isNumeric(s)).Then(() => console.log("s encodes x")),
Else(() => console.log("s does not encode x"))
);
Kotlin's when
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
wenn.js
wenn(x,
Case(inRange(1, 10)).Then(() => console.log("x is in the range")),
Case(inArray(validNumbers)).Then(() => console.log("x is valid")),
Case(not(inRange(10, 20))).Then(() => console.log("x is outside the range")),
Else(() => console.log("none of the above"))
);
Kotlin's when
when {
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}
wenn.js
wenn(true,
Case(x.isOdd()).Then("x is odd"),
Case(x.isEven()).Then("x is even"),
Else("x is funny")
);
npm run test
Special thanks to @MakroCow for helping out on the syntax and beta testing.
MIT
FAQs
A simple but powerful utility function, inspired by Kotlin's when.
We found that wenn 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.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.