https://github.com/anatol-grabowski/incstr
Increment string or generate sequential string ids
https://github.com/anatol-grabowski/incstr
javascript nodejs npm-module
Last synced: 6 months ago
JSON representation
Increment string or generate sequential string ids
- Host: GitHub
- URL: https://github.com/anatol-grabowski/incstr
- Owner: anatol-grabowski
- License: mit
- Created: 2017-05-13T05:37:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-03T17:44:41.000Z (almost 4 years ago)
- Last Synced: 2025-07-16T01:21:02.104Z (6 months ago)
- Topics: javascript, nodejs, npm-module
- Language: JavaScript
- Size: 8.79 KB
- Stars: 80
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# incstr
Increment strings or generate sequential string ids in node.js or browser
## Usage
### incstr
```
const incstr = require('incstr')
nextStr = incstr(str,
[alphabet=incstr.alphabet],
[numberlike=incstr.numberlike])
```
- `str` - string to increment;
- `alphabet` - alphabet to use (default `'A..Za..z0..9'`);
- `numberlike` - `'BA'` after `'9'` instead of `'AA'`(default `false`);
- default `alphabet` can be set through `incstr.alphabet`;
- default value for `numberlike` can be set through `incstr.numberlike`;
- works with strings of any length.
### incstr.idGenerator
```
nextId = incstr.idGenerator(options)
id = nextId() // real generator would be too bulky "nextId.next().value"
```
Possible options:
- `options.lastId`;
- `options.alphabet`;
- `options.numberlike`;
- `options.prefix`;
- `options.suffix`.
`lastId` can also be accessed later through `nextId.lastId` property.
Note that `idGenerator` is more than twice as fast as `incstr`.
## Examples
Pass a string to increment using default alphabet:
```
let i = incstr() // "A"
i = incstr(i) // "B"
...
i = incstr(i) // "9"
i = incstr(i) // "AA"
i = incstr(i) // "AB"
```
Pass a string and an alphabet to use:
```
incstr("ccc", "abc") // "aaaa"
incstr("cc", "ab") // throws ('c' is not in alphabet 'ab')
incstr("0", "01") // "1"
incstr("1", "01") // "00", note NOT "10"
incstr("1", "01", true) // "10", numberlike increment
```
Generate ids:
```
const nextId = incstr.idGenerator()
id1 = nextId() // 'A'
id2 = nextId() // 'B'
```
```
const nextId = incstr.idGenerator({alphabet:'ab', prefix:'id_', suffix:''})
nextId() // 'id_a'
nextId() // 'id_b'
nextId() // 'id_aa'
```
```
const nextId = incstr.idGenerator({lastId:'cc', alphabet:'abc', numberlike: true})
id = nextId() // 'baa'
```