@ampatspell/sofa-util
v0.0.3
Published
Utils for sofa libraries
Downloads
11
Readme
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:[email protected]: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...