1.3.1 โ€ข Published 1 year ago

@sidiousware/dontpanic v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

๐Ÿšฏ dontpanic

npm package Build Status Code Coverage Commitizen Friendly Semantic Release

Introduction

Humans are known to panic. This is widely considered bad design.

function eat(f: string) {
  if (f === '๐ŸŒฏ') ...
  else throw; // panic
}

Panic is not usefulโ„ข. What's useful is a thorough assessment of the circumstances. dontpanic brings you blazingly steadfast abstractions that help you recover from failure and guide your code to success.

DontPanic(eat)('๐ŸŒฏ').onSuccess(sleep).onFailure(getPizza); // ๐Ÿ• Don't panic

Principles

๐Ÿšฏ No littering

Unhandled throws crash your program and send it into an unrecoverable state.

throw new Error('Invalid input'); // ๐Ÿšฏ Crash risk

return Failed('Invalid input'); // ๐Ÿ›ฃ  No crash risk

99.9% of the time there is no need to panic.

โš ๏ธ Explicit content

Throwable functions do not encode potential in their type signature.

const parsed = JSON.stringify(input); // โš ๏ธ Number, may crash

DontPanic(JSON.stringify)(input); // โœ…  Outcome<string, Error>

Explicit is better than implicit.

๐Ÿฅž Flat as a pancake

Try/catch creates new execution scopes.

try {
  const parsed = JSON.parse(input);
  try {
    const validated = validateInput(parsed);
    register(validated);
  } catch (e) {
    handleValidationError(e);
  }
} catch (e) {
  handleParsingError(e); // ๐Ÿชน Far away from home
}

DontPanic(JSON.parse)(input) // ๐Ÿฅž
  .onFailure(handleParsingError)
  .onSuccess(validateInput)
  .onFailure(handleValidationError)
  .onSuccess(register);

Flatten your error handling.

Install

npm install @sidiousware/dontpanic
1.3.1

1 year ago

1.3.0

1 year ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago