Groups array of strings by omitting number characters

§ Quick Take

import { strict as assert } from "assert";
import { groupStr } from "array-group-str-omit-num-char";

  groupStr(["a1-1", "a2-2", "b3-3", "c4-4"]),
    "a*-*": 2,
    "b3-3": 1,
    "c4-4": 1,

§ Idea

Take an array of strings, group those which differ only by a certain number.

For example, consider this array atomic CSS class names coming from some report (for example, email-comb output object's key deletedFromHead):


In real life, you could have for example, wbr425-padding-top-* would be shorter and go up to 500 and there were, let's say, 20 other groups like it.

This npm library groups strings, in this case producing:

"wbr425-padding-top-*": 3,
"main-title": 1

Notice the "425" in wbr425 was not replaced with wildcard because it was constant on all strings that were grouped. This feature, retaining constant digits, was the reason why we got into hassle producing this library.

You see, the quickest, alternative (gung-ho) algorithm is to replace all digits with "*" and filter the unique values, but "425" in wbr425 would be lost. That's why we need this library.


groupStr(sourceArray, [opts])

§ API - Input

Input argumentTypeObligatory?Description
sourceArrayArrayyesArray of zero or more strings
otpsPlain objectnoAn Optional Options Object. See its API below.

By the way, none of the input arguments are mutated.

§ API - Output

An empty array input will give output of a empty plain object. A non-empty array (with at least one string inside) will yield a plain object: strings will be grouped and put as keys, they count will be put as integer values.

For example:

console.log(groupStr(["a1-1", "a2-2", "b3-3", "c4-4"]));
// {
// "a*-*": 2,
// "b3-3": 1,
// "c4-4": 1
// }

§ An Optional Options Object

Type: object - an Optional Options Object.

options object's keyTypeDefaultDescription
wildcardString*What to use to mark grouped characters
dedupePleaseBooleantrueBy default, input array's contents will be deduped. But that's at a cost of performance, so if you're 100% sure your strings will be unique, set it to false.

Here are all defaults in one place for copying:

wildcard: "*",
dedupePlease: true

To explicitly mark the refusal to set custom Optional Options, it can be also passed as a null or undefined value. In that case, defaults will be set.

§ Changelog

See it in the monorepo opens in a new tab, on GitHub.

§ Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 array-of-arrays-sort-by-col 3.0.14
Sort array of arrays by column, rippling the sorting outwards from that column
📦 array-of-arrays-into-ast 2.0.14
Turns an array of arrays of data into a nested tree of plain objects
📦 array-pull-all-with-glob 5.0.14
Like _.pullAll but with globs (wildcards)
📦 array-includes-with-glob 3.0.14
Like _.includes but with wildcards
📦 arrayiffy-if-string 3.13.14
Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else