A utility library for data transformation, type coercion, and value manipulation in JavaScript.
npm install transformation-utils
import {
AddValues,
StrictNumberConverter,
CoerceToType,
StringifyValue,
} from 'transformation-utils';
import { AddValues } from 'transformation-utils/addValues';
import { StrictNumberConverter } from 'transformation-utils/strictNumberConverter';
import { CoerceToType } from 'transformation-utils/coerceToType';
import { StringifyValue } from 'transformation-utils/stringifyValue';
Adds two values with intelligent type handling and fallback to string concatenation.
import { AddValues } from 'transformation-utils';
// Numeric addition
const result1 = new AddValues(5, 3).add(); // 8
const result2 = new AddValues('10', '20').add(); // 30
// String concatenation fallback
const result3 = new AddValues('hello', 'world').add(); // 'helloworld'
const result4 = new AddValues('123', 'abc').add(); // '123abc'
// Array concatenation
const result5 = new AddValues([1, 2], [3, 4]).add(); // [1, 2, 3, 4]
// Object merging
const result6 = new AddValues({ a: 1 }, { b: 2 }).add(); // {a: 1, b: 2}
Converts values to numbers with strict validation.
import { StrictNumberConverter } from 'transformation-utils';
// Valid conversions
const converter1 = new StrictNumberConverter('123');
console.log(converter1.convertToNumber()); // 123
const converter2 = new StrictNumberConverter('3.14');
console.log(converter2.convertToNumber()); // 3.14
// Nested object conversion
const converterNested = new StrictNumberConverter({ value: '456' }.value);
console.log(converterNested.convertToNumber()); // 456
// Invalid conversions throw errors
try {
const converter3 = new StrictNumberConverter('abc');
converter3.convertToNumber(); // Throws error
} catch (error) {
console.log(error.message); // "There is no digits in your string"
}
Coerces values to specific types with validation.
import { CoerceToType } from 'transformation-utils';
// String coercion
const stringResult = new CoerceToType('hello', 'string').coerce(); // 'hello'
// Number coercion
const numberResult = new CoerceToType('123', 'number').coerce(); // 123
// Boolean coercion
const boolResult = new CoerceToType('true', 'boolean').coerce(); // true
// Array coercion
const arrayResult = new CoerceToType('1,2,3', 'array').coerce(); // [1, 2, 3]
// Object coercion
const objectResult = new CoerceToType('{"a": 1}', 'object').coerce(); // {a: 1}
Converts any value to a string representation.
import { StringifyValue } from 'transformation-utils';
const stringifier = new StringifyValue();
// Basic types
console.log(new StringifyValue(42).toString()); // "42
console.log(new StringifyValue(null)); // "null"
// Objects and arrays
console.log(new StringifyValue({ a: 1, b: 2 })).toString(); // '{"a":1,"b":2}'
// Functions
console.log(new StringifyValue(() => {})).toString; // "function"
The library includes a centralized error handling system:
import { error } from 'transformation-utils';
// The error helper is used internally by the classes
// You can also use it in your own code if needed
error('Custom error message');
Run the test suite:
npm test
Run tests with coverage:
npm run test:coverage
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
ISC License - see LICENSE file for details.
Natalia Novikova novikovanatalie@protonmail.com