Skip to content

Commit

Permalink
fix: exit early if number less than one is given
Browse files Browse the repository at this point in the history
fix #42
  • Loading branch information
revelt committed Mar 21, 2022
1 parent 0609f4e commit dc4ffc1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
78 changes: 72 additions & 6 deletions packages/string-remove-thousand-separators/src/main.ts
Expand Up @@ -4,6 +4,7 @@ import trimChars from "lodash.trim";

import { version as v } from "../package.json";

declare let DEV: boolean;
const version: string = v;

interface Opts {
Expand All @@ -13,7 +14,6 @@ interface Opts {
}

function remSep(str: string, originalOpts?: Partial<Opts>): string {
// vars
let allOK = true; // used to bail somewhere down the line. It's a killswitch.
let knownSeparatorsArray = [".", ",", "'", " "];
let firstSeparator;
Expand Down Expand Up @@ -51,24 +51,47 @@ function remSep(str: string, originalOpts?: Partial<Opts>): string {

// end sooner if it's an empty string:
if (res === "") {
DEV && console.log(`054 early return`);
return res;
}

if (+str > 0 && +str < 1) {
DEV && console.log(`059 early return - less than 1`);
return str;
}

// we'll manage the TO-DELETE string slice ranges using this:
let rangesToDelete = new Ranges();

// traverse the string indexes
for (let i = 0, len = res.length; i < len; i++) {
// Logging:
// -------------------------------------------------------------------------
DEV &&
console.log(
`\u001b[${36}m${`===============================`}\u001b[${39}m \u001b[${35}m${`str[ ${i} ] = ${
str[i]?.trim() ? str[i] : JSON.stringify(str[i], null, 4)
}`}\u001b[${39}m \u001b[${36}m${`===============================`}\u001b[${39}m\n`
);

// -------------------------------------------------------------------------
// catch empty space for Russian-style thousand separators (spaces):
if (opts.removeThousandSeparatorsFromNumbers && res[i].trim() === "") {
DEV &&
console.log(
`082 ${`\u001b[${33}m${`ADD`}\u001b[${39}m`} [${i}, ${i + 1}]`
);
rangesToDelete.add(i, i + 1);
}
// -------------------------------------------------------------------------
// catch single quotes for Swiss-style thousand separators:
// (safe to delete instantly because they're not commas or dots)
if (opts.removeThousandSeparatorsFromNumbers && res[i] === "'") {
rangesToDelete.add(i, i + 1);
DEV &&
console.log(
`093 ${`\u001b[${33}m${`ADD`}\u001b[${39}m`} [${i}, ${i + 1}]`
);
// but if single quote follows this, that's dodgy and let's bail
if (res[i + 1] === "'") {
// bail!
Expand All @@ -81,17 +104,25 @@ function remSep(str: string, originalOpts?: Partial<Opts>): string {
// catch thousand separators
if (knownSeparatorsArray.includes(res[i])) {
// check three characters to the right
if (res[i + 1] !== undefined && /^\d*$/.test(res[i + 1])) {
if (res[i + 1] !== undefined && /^\d$/.test(res[i + 1])) {
if (res[i + 2] !== undefined) {
if (/^\d*$/.test(res[i + 2])) {
if (/^\d$/.test(res[i + 2])) {
//
// thousands separator followed by two digits...
if (res[i + 3] !== undefined) {
if (/^\d*$/.test(res[i + 3])) {
if (res[i + 4] !== undefined && /^\d*$/.test(res[i + 4])) {
if (/^\d$/.test(res[i + 3])) {
if (res[i + 4] !== undefined && /^\d$/.test(res[i + 4])) {
// four digits after thousands separator
// bail!
allOK = false;
DEV &&
console.log(
`120 ${`\u001b[${32}m${`SET`}\u001b[${39}m`} ${`\u001b[${33}m${`allOK`}\u001b[${39}m`} = ${JSON.stringify(
allOK,
null,
4
)}; break`
);
break;
} else {
//
Expand All @@ -100,6 +131,12 @@ function remSep(str: string, originalOpts?: Partial<Opts>): string {
// 1. submit for deletion
if (opts.removeThousandSeparatorsFromNumbers) {
rangesToDelete.add(i, i + 1);
DEV &&
console.log(
`136 ${`\u001b[${33}m${`ADD`}\u001b[${39}m`} [${i}, ${
i + 1
}]`
);
}

// 2. enforce the thousands separator consistency:
Expand Down Expand Up @@ -131,6 +168,12 @@ function remSep(str: string, originalOpts?: Partial<Opts>): string {
//
// If Russian notation:
rangesToDelete.add(i, i + 1, ".");
DEV &&
console.log(
`173 ${`\u001b[${33}m${`ADD`}\u001b[${39}m`} [${i}, ${
i + 1
}, "."]`
);
}
} else {
// stuff like "1,0a" - bail
Expand All @@ -146,26 +189,49 @@ function remSep(str: string, originalOpts?: Partial<Opts>): string {
// Convert Russian notation if requested:
if (opts.forceUKStyle && res[i] === ",") {
rangesToDelete.add(i, i + 1, ".");
DEV &&
console.log(
`194 ${`\u001b[${33}m${`ADD`}\u001b[${39}m`} [${i}, ${
i + 1
}, "."]`
);
}
// Pad it with zero if requested:
if (opts.padSingleDecimalPlaceNumbers) {
rangesToDelete.add(i + 2, i + 2, "0");
DEV &&
console.log(
`204 ${`\u001b[${33}m${`ADD`}\u001b[${39}m`} [${i + 2}, ${
i + 2
}, "0"]`
);
}
}
}
// when we have one decimal place, like "100.2", we pad it to two places, like "100.20"
// -------------------------------------------------------------------------
} else if (!/^\d*$/.test(res[i])) {
} else if (!/^\d$/.test(res[i])) {
// catch unrecognised characters,
// then turn off the killswitch and break the loop
allOK = false;
DEV &&
console.log(
`219 ${`\u001b[${32}m${`SET`}\u001b[${39}m`} ${`\u001b[${33}m${`allOK`}\u001b[${39}m`} = ${JSON.stringify(
allOK,
null,
4
)}`
);
break;
}
}

if (allOK && rangesToDelete.current()) {
DEV && console.log(`230 RETURN`);
return rApply(res, rangesToDelete.current());
}

DEV && console.log(`234 RETURN`);
return res;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/string-remove-thousand-separators/test/etc.js
@@ -0,0 +1,11 @@
import { test } from "uvu";
// eslint-disable-next-line no-unused-vars
import { equal, is, ok, throws, type, not, match } from "uvu/assert";

import { remSep as r } from "../dist/string-remove-thousand-separators.esm.js";

test("01", () => {
equal(r("0.075"), "0.075", "01");
});

test.run();

0 comments on commit dc4ffc1

Please sign in to comment.