dynamic-typing
v1.0.1
Published
Dynamically type a string
Downloads
9,419
Readme
dynamic-typing
Dynamically types a string.
When parsing text files, like the ones coming out fromm scientific instruments, it is often useful to convert the obtained strings to their corresponding types.
Indeed when you parse the file while all the fields are text some of them represents in fact numbers or booleans.
This package will try to make the conversion for you using the following rules:
- a string that has as lowercase value 'true' or 'false' will be converted to the corresponding boolean
- a string that when converted to number (using
Number(string)
) does not yield to NaN will be converted to number - other strings will be kept as string
This package was optimized for speed and seems to work pretty well. Please don't hesitate to submit bug reports or contribute.
Installation
$ npm i dynamic-typing
Usage
import { parseString } from 'dynamic-typing';
const result = parseString('0x100');
// result is the number 256
More examples:
parseString('')
➡''
parseString(' ')
➡' '
parseString('.')
➡'.'
parseString('0.')
➡0
parseString('0.0')
➡0
parseString('abc')
➡'abc'
parseString('True')
➡true
parseString('false')
➡false
parseString('0')
➡0
parseString('123')
➡123
parseString('123.456')
➡123.456
parseString('12e3')
➡12000
parseString('0x10')
➡16
parseString('0b10')
➡2