js-free-float-parse
TypeScript icon, indicating that this package has built-in type declarations

2.1.6 • Public • Published

js-free-float-parse

js-free-float-parse is a JavaScript library built on top of decimal.js that designed to parse and convert user/html input strings into numeric values, handling various edge cases such as leading zeros typing and formatting options.

For test-cases see tests/index.spec.ts

Installation

npm install js-free-float-parse

Usage

import jsFreeFloatParse from 'js-free-float-parse';

const options = {
  min: 0,
  max: 100,
  dot: true,
  precision: 2
};

const [string, number] = jsFreeFloatParse('12,34', options);

console.log(string); // "12.34"
console.log(number); // 12.34

Options

type Options = {
  /**
   * The minimum allowable value. Defaults to -Infinity.
   * */
  min?: number
  /**
   * The maximum allowable value. Defaults to Infinity.
   * */
  max?: number
  /**
   * If set to true, the function will use a dot as the decimal separator.
   * If false, it will use a comma. Defaults to comma.
   * */
  dot?: boolean
  /**
   * The number of decimal places to include in the output. If not specified, the original precision is preserved.
   * */
  precision?: number
}

Testing

Library uses vitest for testing

npm run test

Edge Cases

The function handles various edge cases, such as:

  • Empty input
  • Single decimal separators (dot or comma)
  • Negative values
  • Leading zeros
  • Multiple decimal separators
  • e-/e+ values like (5.5e-10)

Readme

Keywords

none

Package Sidebar

Install

npm i js-free-float-parse

Weekly Downloads

3

Version

2.1.6

License

MIT

Unpacked Size

58.8 kB

Total Files

20

Last publish

Collaborators

  • vladrose