
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
sweet-sugar
Advanced tools
The match statement works by starting off with a key to match followed by multiple chaining methods.
match(`hello`)
.when('hello', () => 'en')
.when('hallo', () => 'de')
.when('salut', () => 'fr')
.when('ciao', () => 'it')
.when('你好', () => 'zh')
.otherwise(language => { throw new Error(`unsupported language: ${language}`) })
.finish //= en
The match statement also supports deep pattern matching & narrowing like so:
type User = {
name: string,
age: number,
role: 'admin' | 'moderator' | 'user',
permissions?: {
moderation?: boolean
}
}
const user: User = {
name: 'John Doe',
age: 35,
role: 'user',
permissions: {
moderation: true
}
}
const canModerate = match(user)
.when({ role: 'admin' }, () => true)
.when({ role: 'moderator' }, () => true)
.when({ // Only match when it's a normal user & they have been granted a special permission
role: 'user',
permissions: {
moderation: true
}
}, () => true)
.otherwise(() => false)
.finish //= true
An option is a type to represent some kind of data that may not exist in a safer way than undefined/null.
const user: Option<User> = await getUser()
console.log(
user.unwrap$() //=> throws an error when user is none
)
A class to improve JS error handling by forcing you to either handle or explicitly supress errors.
const user: Result<User, ApiError> = await getUser()
match(user)
.ok(user => {
alert(`your name is ${user.name}`)
})
.error(error => {
console.error(`something went wrong: ${error}`)
})
Some sugar that allows you to chain methods in a more concise way.
import 'sweet-sugar/let'
'my string'.let(console.log)
// so instead of
myFunctionProcessingTheReplacedValue('Hello World'.replace('o', '0')).charAt(0)
// you can write
'Hello World'.replace('o', '0').let(myFunctionProcessingTheReplacedValue).charAt(0)
'mid-chain logging'.replace('-', '_').let(console.log).replace('_', '-')
A function that allows you to use the building pattern on classes which don't support it themselves.
class MyClass { ... }
const instance = build(MyClass, 'my constructor argument')
.set('test', true)
.update('doSomething', old => (...args: Array<any>) => {
console.log('before')
const result = old(...args) //=> 42
console.log('after')
return result
})
instance.test //=> true
instance.doSomething()
// before
// after
//=> 42
FAQs
A collection of tasty syntax utilities mostly inspired by Rust
We found that sweet-sugar 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.