JSPM

  • Created
  • Published
  • Downloads 916
  • Score
    100M100P100Q93426F
  • License MIT

Groups array of strings by omitting number characters

Package Exports

  • array-group-str-omit-num-char

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 (array-group-str-omit-num-char) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

array-group-str-omit-num-char

Groups array of strings by omitting number characters

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 array-group-str-omit-num-char
// consume as CommonJS require():
const group = require("array-group-str-omit-num-char");
// or as ES Module:
import group from "array-group-str-omit-num-char";

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/array-group-str-omit-num-char.cjs.js 3 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/array-group-str-omit-num-char.esm.js 3 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/array-group-str-omit-num-char.umd.js 41 KB

⬆ back to top

Table of Contents

Idea

Take an array of strings and if they differ only in number characters, group them.

For example, consider this input - array of strings:

[
  "aaaaaa9-1"
  "aaaaaa9-2"
  "bbbbbb"
  "aaaaaa9-3"
]

Output - plain object:

{ "aaaaaa9-*": 3 "bbbbbb": 1 }

Notice the aaa... were grouped and 9 was not replaced with wildcard because it was constant on all strings. This feature, retaining constant digits, was the reason why we got into hassle producing this library.

Practically, we're going to use it to filter unused class and id names which were removed in emailcomb.com. The list can get quite long, 7000 or more unused atomic classes and id's are not uncommon. To make such lists more manageable, this library will be used to group them, to reduce the amount of entries in the list.

⬆ back to top

API

group(
  sourceArray, // input array of strings
  opts // an optional options array
);

API - Input

Input argument Type Obligatory? Description
sourceArray Array yes Source array of strings
otps Plain object no An Optional Options Object. See its API below.

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

⬆ back to top

An Optional Options Object

Type: object - an Optional Options Object.

options object's key Type Default Description
{
wildcard String * What to use to mark grouped characters
dedupePlease Boolean true By 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.

⬆ back to top

API - Output

Empty array input will yield empty plain object as result. 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(group(["a1-1", "a2-2", "b3-3", "c4-4"]));
// {
//   "a*-*": 2,
//   "b3-3": 1,
//   "c4-4": 1
// }

⬆ back to top

Contributing

  • If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.

  • If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.

  • If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.

⬆ back to top

Licence

MIT License (MIT)

Copyright © 2018 Codsen Ltd, Roy Revelt