JSPM

  • Created
  • Published
  • Downloads 455
  • Score
    100M100P100Q110858F
  • License MIT

Detects and removes thousand separators (dot/comma/quote/space) from string-type digits

Package Exports

  • string-remove-thousand-separators

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-remove-thousand-separators) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

string-remove-thousand-separators

Detects and removes thousand separators (dot/comma/quote/space) from string-type digits

Minimum Node version required Repository is on BitBucket Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Install

npm i string-remove-thousand-separators
// consume as a CommonJS require:
const remSep = require("string-remove-thousand-separators");
// or as an ES Module:
import remSep from "string-remove-thousand-separators";

// feed a numeric string to it:
let res = remSep("100,000.01"); // => 100000.01

Here's what you'll get:

Type Key in package.json Path Size
Main export - CommonJS version, transpiled to ES5, contains require and module.exports main dist/string-remove-thousand-separators.cjs.js 4 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/string-remove-thousand-separators.esm.js 4 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/string-remove-thousand-separators.umd.js 39 KB

⬆ back to top

Table of Contents

Purpose

This library detects and removes a thousand separators from numeric strings.

The main consumer will be csv-split-easy which deals with exported Internet Banking CSV files in a double-entry accounting format.

The numeric string must be NUMERIC, that is, not contain any letters or other unrelated characters. It can contain empty space though, which will be automatically trimmed.

⬆ back to top

Examples

var remSep = require("string-remove-thousand-separators");

// πŸ‡¬πŸ‡§ πŸ‡ΊπŸ‡Έ thousand separators:
console.log(remSep("1,000,000.00"));
// => "1000000.00"

// πŸ‡·πŸ‡Ί  thousand separators:
console.log(remSep("1 000 000,00"));
// => "1000000,00"
// (if you want it converted to Western notation with dot,
// set opts.forceUKStyle = true, see below)

// πŸ‡¨πŸ‡­ thousand separators:
console.log(remSep("1'000'000.00"));
// => "1000000.00"

// IT'S SMART TOO:

// will not delete if the thousand separators are mixed:
console.log(remSep("100,000,000.000")); // => does nothing

// but will remove empty space, even if there is no decimal separator:
// (that's to cope with Russian notation integers that use thousand separators)
console.log(remSep("100 000 000 000")); // => 100000000000

// while removing thousand separators, it will also pad the digits to two decimal places
// (optional, on by default, to turn it off set opts.padSingleDecimalPlaceNumbers to `false`):
console.log(remSep("100,000.2"));
// => "100000.20" (Western notation)

console.log(remSep("100 000,2"));
// => "100000,20" (Russian notation)

console.log(remSep("100'000.2"));
// => "100000.20" (Swiss notation)

⬆ back to top

API

remSep('str'[, opts])

If first argument (input) is not string, it will throw and error. Second input argument, opts, is optional. However, if it is present and is not null, not undefined and not a plain object, it will throw and error.

⬆ back to top

options

Defaults:

    {
      removeThousandSeparatorsFromNumbers: true,
      padSingleDecimalPlaceNumbers: true,
      forceUKStyle: false
    }
options object's key Type Obligatory? Default Description
{
removeThousandSeparatorsFromNumbers Boolean no true Should remove thousand separators? 1,000,000 β†’ 1000000? Or Swiss-style, 1'000'000 β†’ 1000000? Or Russian-style, 1 000 000 β†’ 1000000?
padSingleDecimalPlaceNumbers Boolean no true Should we pad one decimal place numbers with zero? 100.2 β†’ 100.20?
forceUKStyle Boolean no false Should we convert the decimal separator commas into dots? 1,5 β†’ 1.5?
}

⬆ back to top

Algorithm

This library uses my new favourite, string trickle-class ("strickle-class") algorithm. The string is looped (we aim once, but it depends on the complexity of the task) and the characters trickle one-by-one through our "traps" where we flip boolean flags and count stuff accordingly to what's passing by.

That's a different approach from using regexes. Regexes are an easy solution when it is possible to achieve it using them. However, most of the cases, limits of regexes dictate the limits of the algorithms, and as a result, we sometimes see crippled web apps that are not smart and not really universal. For example, when we banked with Metro Bank back in 2015, they used to export CSV's with some numbers having a thousand separators and some not having them. Also, separately from that, some numbers were wrapped with double quotes, and some were not. That drew my accounting software crazy, and we had to manually edit the CSV's each time. Funnily, a combination of this library and csv-split-easy would solve such issues. The question is, how come corporate software can't do things that open source can? Is it corporate ceilings of all kinds or is it the power of JavaScript?

⬆ back to top

Contributing

  • If you see an error, raise an issue.
  • If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
  • If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
  • If you want to contribute some code, fork the monorepo via BitBucket, then write code, then file a pull request via BitBucket. We'll merge it in and release.

In monorepo, npm libraries are located in packages/ folder. Inside, the source code is located either in src/ folder (normal npm library) or in the root, cli.js (if it's a command line application).

The npm script "dev", the "dev": "rollup -c --dev --silent" builds the development version retaining all console.logs with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.logs.

⬆ back to top

Licence

MIT License

Copyright (c) 2015-2019 Roy Revelt and other contributors