New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

sweet-sugar

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sweet-sugar

A collection of tasty syntax utilities mostly inspired by Rust

latest
Source
npmnpm
Version
1.10.3
Version published
Maintainers
1
Created
Source

🍫Sweet Sugar

A collection of tasty syntax utilities mostly inspired by Rust

List of features

  • Match Statement
  • Builder
  • Option
  • Result
  • Let

Match Statement

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

Advanced Examples

Option

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
)

Advanced Examples

Result

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}`)
  })

Advanced Examples

Let

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('_', '-')

Advanced Examples

Builder

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

Advanced Examples

Keywords

syntax

FAQs

Package last updated on 21 Dec 2023

Did you know?

Socket

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.

Install

Related posts