https://github.com/qpssoft/auto-group-strings-array
Small JS library to group array of strings by common substring
https://github.com/qpssoft/auto-group-strings-array
library
Last synced: about 1 year ago
JSON representation
Small JS library to group array of strings by common substring
- Host: GitHub
- URL: https://github.com/qpssoft/auto-group-strings-array
- Owner: qpssoft
- License: mit
- Created: 2024-03-08T03:44:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-28T13:18:54.000Z (over 2 years ago)
- Last Synced: 2025-02-23T22:36:22.535Z (over 1 year ago)
- Topics: library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/auto-group-strings-array
- Size: 653 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# auto-group-strings-array
[](https://github.com/qpssoft/auto-group-strings-array/actions?query=workflow%3Atest) [](https://codecov.io/github/qpssoft/auto-group-strings-js?branch=master) [](https://snyk.io/test/github/qpssoft/auto-group-strings-js) [](https://nodejs.org)
Small JS library to group array of strings by common substring
## Node.js
`npm install auto-group-strings-array`
## Browser
Use `auto-group-strings.min.js` file from [dist/](dist/)
### Function Arguments:
1. inputStrings (type: `Array`)
2. options, type: `Object` (optional), properties:
- **delimiter** (type: `string`, default: `" "`)
- **delimiterRegExp** (type: `RegExp`, default: `undefined`)
- if **delimiterRegExp** is provided, **delimiter** (`string`) will only be used as a fallback when there is no match for **delimiterRegExp**
- **direction** (type: `string`, default: `"rtl"`)
- Its possible values are `"ltr"` for searching left to right or, `"rtl"` for right to left.
- **caseSensitive** (type: `boolean`, default: `false`)
- **includeSingleElementMembers** (type: `boolean`, default: `false`)
- this option includes every input string from the first argument as **common** and at least one element (index) in **members** array.
### Return Type:
- `Array` where
- **`common`** property is a `string`
- **`members`** property is an `Array`
## Usage
```js
const autoGroupStrings = require("auto-group-strings");
const result = autoGroupStrings(
[
"hello code", // 0
"apple and orange", // 1
"for the happy code", // 2
"i don't know", // 3
"is it?", // 4
"it's a happy code", // 5
],
{
delimiter: " ",
direction: "rtl",
},
);
console.log(result);
/*
[
{ common: 'code', members: [ 0, 2, 5 ] },
{ common: 'happy code', members: [ 2, 5 ] }
]
*/
```
- For more examples, please check [examples](examples/) directory.