Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonh1701/textline-sorter
A flexible utility for sorting lines of text.
https://github.com/simonh1701/textline-sorter
cjs commonjs ecmascript esm javascript nodejs npm sort sorter sorting typescript
Last synced: 3 months ago
JSON representation
A flexible utility for sorting lines of text.
- Host: GitHub
- URL: https://github.com/simonh1701/textline-sorter
- Owner: simonh1701
- License: mit
- Created: 2024-10-18T18:05:52.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-10-20T06:58:03.000Z (4 months ago)
- Last Synced: 2024-10-20T10:52:56.593Z (3 months ago)
- Topics: cjs, commonjs, ecmascript, esm, javascript, nodejs, npm, sort, sorter, sorting, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/textline-sorter
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# textline-sorter
A flexible utility for sorting lines of text.
## Features
- Sort lines alphabetically, alphanumerically, or by length
- Ascending or descending sort order
- Option to trim whitespace
- Option to remove empty lines
- Option to remove duplicate lines## Installation
```bash
npm install textline-sorter
```## API
### `sortLines(text: string, options: SortOptions): string`
Sorts the lines of the input text based on the provided options.
#### Parameters
- `text`: The input text to sort.
- `options`: An object of type `SortOptions` specifying the sorting behavior.#### SortOptions
- `order`: `"alphabetical" | "alphanumerical" | "length"` - The sorting method to use.
- `direction`: `"asc" | "desc"` - The sort direction (ascending or descending).
- `caseFirst`: `"upper" | "lower"` (optional, default: `"upper"`) - Whether upper case letters should be sorted before lower case letters (`"upper"`) or vice versa (`"lower"`).
- `trimWhitespace`: `boolean` (optional, default: `false`) - Whether to trim whitespace from the beginning and end of each line before sorting.
- `removeEmptyLines`: `boolean` (optional, default: `false`) - Whether to remove empty lines before sorting.
- `removeDuplicateLines`: `boolean` (optional, default: `false`) - Whether to remove duplicate lines before sorting.## Usage
### Example 1: Sorting Superheroes
```typescript
import { sortLines, SortOptions } from "textline-sorter";const superheros: string = `Thor
Aquaman
Captain America
Scarlet Witch
Black Panther
AquamanScarlet Witch
Cyborg
Black Widow
Captain America
Deadpool
The Flash
Spider-ManBatman
Green Arrow
Wolverine
Green Arrow
The Flash`;const options: SortOptions = {
order: "alphabetical",
direction: "asc",
removeEmptyLines: true,
removeDuplicateLines: true,
};const sortedSuperheros = sortLines(superheros, options);
console.log(sortedSuperheros);
```Output:
```
Aquaman
Batman
Black Panther
Black Widow
Captain America
Cyborg
Deadpool
Green Arrow
Scarlet Witch
Spider-Man
The Flash
Thor
Wolverine
```For more examples, please refer to [/docs/USAGE.md](https://github.com/simonh1701/textline-sorter/blob/main/docs/USAGE.md).
## License
MIT