Transformation

Table of Content
Purpose
Collection of helpful functions for data transformation
Installation
Via npm
npm install @scuba-squad/transformation
API
toArray(value: mixed, opt: ?object = {flatten: false}): array
Added in: v1.0.0
Convert value
to an array
arguments:
value: mixed
opt: ?object
returns: array
const Transformation = require('@scuba-squad/transformation');
Transformation.toArray('hello world');
Transformation.toArray({a: 5, b: true});
Transformation.toArray(new Set([1, 2, 3]));
toAscii(value: mixed): string
Added in: v1.0.0
Convert value
to an ascii string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toAscii('bismi ăl-lahi ăr-raḥmani ăr-raḥiymi');
Transformation.toAscii(['$', 'vs', '€']);
toBase64(value: mixed): string
Added in: v1.0.0
Convert value
to a base64 encoded string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toBase64('scubaSquad');
Transformation.toBase64(NaN);
Transformation.toBase64([null, false, {}, [4]]);
toBoolean(value: mixed): boolean
Added in: v1.0.0
Convert value
to a boolean
arguments:
returns: boolean
const Transformation = require('@scuba-squad/transformation');
Transformation.toBoolean('hi');
Transformation.toBoolean({a: 5});
Transformation.toBoolean('false');
Transformation.toBoolean('off');
Transformation.toBoolean('no');
Transformation.toBoolean(' ');
toCamelCase(value: mixed): string
Added in: v1.0.0
Convert value
to a camelCase string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toCamelCase('-person- -');
Transformation.toCamelCase('JSON_2_XML');
Transformation.toCamelCase('@scuba-squad/transformation:toCamelCase');
Transformation.toCamelCase(['scuba', 'squad']);
toCSV(value: mixed, opt: ?object = {}): string
Added in: v1.0.0
see: json2csv
Convert value
to a CSV string
arguments:
value: mixed
opt: ?object
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toCSV([{a: 5}, {b: 4}, {a: 3}]);
toFormat(value: string, ...args: mixed): string
Added in: v1.0.0
see: util.format
Convert value
to a string replacing format placeholders with arg[n] value
arguments:
value: string
...args: mixed
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toFormat('%s: %n', 'number', 5);
toJSON(value: mixed, space: string | number = 2): string
Added in: v1.0.0
Convert value
to serialized json
arguments:
value: mixed
space: string | number = 2
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toJSON({a: 5});
Transformation.toJSON({a: 5}, 0);
Transformation.toJSON({a: 5}, '\t');
toKebabCase(value: mixed): string
Added in: v1.0.0
Convert value
to kebab-case string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toKebabCase('@scuba-squad/transformation:toKebabCase');
Transformation.toKebabCase(['scuba', 'squad']);
toLines(value: mixed): array
Added in: v1.0.0
Convert value
to an array of lines
arguments:
returns: array
const Transformation = require('@scuba-squad/transformation');
Transformation.toLines('1\n2\r\n3');
Transformation.toLines('1\n\n2\r\n3\n');
toList(value: array, opt: ?object = {delimiter: ?string = ', ', last: ?string = ' and '}): string
Added in: v1.0.0
Convert value
to a list string
arguments:
value: array
compare: ?object
delimiter: string = ', '
last: string = ' and '
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toList([1, 2, 3]);
Transformation.toList([127, 0, 0, 1], {
delimiter: '.',
last: '.',
});
Transformation.toList([1, 0, 4, 'beta'], {
delimiter: '.',
last: '-',
});
toListSerial(value: array, opt: ?object = {delimiter: ?string = ', ', last: ?string = ' and '}): string
Added in: v1.0.0
Convert value
to a serial list string
arguments:
value: array
compare: ?object
delimiter: string = ', '
last: string = ' and '
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toListSerial([1, 2, 3]);
Transformation.toListSerial(['apples', 'oranges', 'bananas'], {
delimiter: '; ',
});
Transformation.toListSerial(['alpha', 'beta', 'rc', 'release'], {
delimiter: '-',
last: '>',
});
toMD5(value: mixed): string
Added in: v1.0.0
Convert value
to an md5 encoded hash string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toMD5('scubaSquad');
Transformation.toMD5({a: 5});
Transformation.toMD5([null, false, {}, [4]]);
toNumber(value: mixed): number
Added in: v1.0.0
Convert value
to number
arguments:
returns: number
const Transformation = require('@scuba-squad/transformation');
Transformation.toNumber('3.5');
Transformation.toNumber(false);
Transformation.toNumber(true);
Transformation.toNumber(new Date('2000-01-01'));
Transformation.toNumber('hello');
toPascalCase(value: mixed): string
Added in: v1.0.0
Convert value
to PascalCase string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toPascalCase('@scuba-squad/transformation:toPascalCase');
Transformation.toPascalCase(['scuba', 'squad']);
toPlural(value: string): string
Added in: v1.0.0
see: pluralize
Convert value
to a plural string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toPlural('car');
Transformation.toPlural('wife');
Transformation.toPlural('woman');
toRegExp(value: mixed): RegExp
Added in: v1.0.0
Convert value
to a RegExp
arguments:
returns: RegExp
const Transformation = require('@scuba-squad/transformation');
Transformation.toRegExp(/asd/i);
Transformation.toRegExp('|asd|i');
Transformation.toRegExp('%^\\d+$%g');
toSingular(value: string): string
Added in: v1.0.0
see: pluralize
Convert value
to a singular string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toSingular('cars');
Transformation.toSingular('wives');
Transformation.toSingular('women');
toSlug(value: mixed): string
Added in: v1.0.0
Convert value
to a slug
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toSlug('helloWorld');
Transformation.toSlug('$ vs €');
Transformation.toSlug(['$', 'vs', '€']);
toSnakeCase(value: mixed): string
Added in: v1.0.0
Convert value
to snake_case string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toSnakeCase('@scuba-squad/transformation:toSnakeCase');
Transformation.toSnakeCase(['scuba', 'squad']);
toString(value: mixed): string
Added in: v1.0.0
Convert value
to a string
arguments:
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toString([1, 2, 3]);
Transformation.toString({a: 5});
Transformation.toString();
toTokens(value: string, delimiter: RegExp | string = /\s+/): array
Added in: v1.0.0
Convert value
to an array of tokens
arguments:
value: mixed
delimiter: ?RegExp | string = /\s+/
returns: array
const Transformation = require('@scuba-squad/transformation');
Transformation.toTokens('fruit: apples, oranges, bananas');
Transformation.toTokens('fruit: apples, oranges, bananas', /\W+/);
toWords(value: mixed): array
Added in: v1.0.0
Convert value
to an array of words
arguments:
returns: array
const Transformation = require('@scuba-squad/transformation');
Transformation.toWords('fruit: apples, oranges, bananas');
Transformation.toWords('this is $1.00');
Transformation.toWords('a,b,c');
towrap(value: mixed, opt: {start: ?string, end: ?string} = {}): string
Added in: v1.0.0
Convert value
to a wrapped string
arguments:
value: mixed
opt: ?object
start: ?string
beginning value to wrap the string
end: ?string
closing value to wrap the string
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toWrap('hello');
Transformation.toWrap('tag', {start: '<', end: '>'});
Transformation.toWrap([1,2,3], {start: '[', end: ']'});
Transformation.toWrap('regex', {start: '/'});
Transformation.toWrap('quote', {end: '"'});
toXML(value: mixed, opt: {spaces: number, compact: boolean, indentCdata: boolean} = {spaces: 2, compact: true, indentCdata: true}): string
Added in: v1.0.0
see: xml-js
Convert value
to a XML string
arguments:
value: mixed
opt: ?object
spaces: 2
compact: true
indentCdata: true
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toXML({name: {first: 'James', last: 'Bond'}, age: 55});
toYAML(value: mixed, opt: {skipinvalid: boolean} = {skipInvalid: true}): string
Added in: v1.0.0
see: js-yaml
Convert value
to a YAML string
arguments:
value: mixed
opt: ?object
returns: string
const Transformation = require('@scuba-squad/transformation');
Transformation.toXML({name: {first: 'James', last: 'Bond'}, age: 55});
Test
tests
npm install
npm test
License
MIT