@ampatspell/sofa-util

0.0.3 • Public • Published

sofa-util

Basic helpers for sofa libraries.

export {
  defineInternal,
  assert,
  defined,
  hidePassword,
  readFile,
  SofaError
};

defineInternal

import { defineInternal } from '@ampatspell/sofa-util';

class Foo {
  constructor(opts = {}) {
    defineInternal(this, {
      name: opts.name
    });
  }

  get name() {
    return this._internal.name;
  }
}

assert

assert helpers. throws AssertError with message, error and reason where error is "internal".

import { assert } from '@ampatspell/sofa-util';

const {
  internal
} = assert;

internal.notBlank(value, 'name');
internal.isString(value, 'name');
internal.isOptionalObject(value, 'opts');
internal.isObject(value, 'opts');

internal.ok(foo === bar, 'foo must equal bar');
internal.fail('%s is not ok', value);

custom error

import { assert } from '@ampatspell/sofa-util';

let database = new assert.Assert('database');

database.notBlank('', 'something');

defined

defined({ ok: true, error: null, name: undefined })
// => { ok: true, error: null }

hidePassword

hidePassword('http://foo:bar@127.0.0.1:5984')
// => 'http://foo:***@127.0.0.1:5984'

readFile

readFile('foo.txt').then(buffer => {
});

SofaError

let error = new SofaError('Not found', {
  status: 404,
  error: 'not_found',
  reason: 'missing'
});

error.toJSON() // => { status: 404, error: 'not_found...

Readme

Keywords

Package Sidebar

Install

npm i @ampatspell/sofa-util

Weekly Downloads

2

Version

0.0.3

License

ISC

Last publish

Collaborators

  • ampatspell