@speedup/string-to
v1.0.4
Published
Convert all the string variables into their actual data type using different parsers
Downloads
32
Maintainers
Readme
@SpeedUP/string-to
Convert all the string values in an array, an object or a single property to the best primitive type.
Installation
# NPM
npm i @speedup/string-to --save
# Yarn
yarn install @speedup/string-to
Usage
Javascript
const { tryAnyToPrimitive, parseArrayItemsSync, parseObjectKeysSync, } = require('@speedup/string-to');
// supports single-level array as well as N-level array
// check test/index.spec.ts to see more examples
const originalArrayD1 = [
'123456',
'0.123456',
'yes',
'November 2018',
null,
undefined,
];
const parsedArray = parseArrayItemsSync(originalArrayD1);
/*
parsedArray = [
123456,
0.123456,
true,
new Date('November 2018'),
null,
undefined,
]
*/
// supports single-level object as well as N-level object
// check test/index.spec.ts to see more examples
const originalObjectD1 = {
name: 'John Doe',
age: '35',
birthday: 'November 1985',
married: 'yes',
};
const parsedObject = parseObjectKeysSync(originalObjectD1);
/*
parsedArray = {
name: 'John Doe',
age: 35,
birthday: new Date('November 1985'),
married: true,
}
*/
TypeScript
import { tryAnyToPrimitive, parseArrayItemsSync, parseObjectKeysSync, } from '@speedup/string-to';
// supports single-level array as well as N-level array
// check test/index.spec.ts to see more examples
const originalArrayD1 = [
'123456',
'0.123456',
'yes',
'November 2018',
null,
undefined,
];
const parsedArray = parseArrayItemsSync(originalArrayD1);
/*
parsedArray = [
123456,
0.123456,
true,
new Date('November 2018'),
null,
undefined,
]
*/
// supports single-level object as well as N-level object
// check test/index.spec.ts to see more examples
const originalObjectD1 = {
name: 'John Doe',
age: '35',
birthday: 'November 1985',
married: 'yes',
};
const parsedObject = parseObjectKeysSync(originalObjectD1);
/*
parsedArray = {
name: 'John Doe',
age: 35,
birthday: new Date('November 1985'),
married: true,
}
*/
And you're good to go!
License
MIT