Skip to content

Commit 178c409

Browse files
committed
Remove babel, use native generators, update readme, bump version
1 parent 8f0decf commit 178c409

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# css-class-generator
22

3-
Generates a sequential valid css class.
3+
Generates a sequential, valid CSS class, generating the next smallest class names possible.
44

55
```bash
66
npm install --save css-class-generator
@@ -10,26 +10,23 @@ npm install --save css-class-generator
1010

1111
`cssNameGenerator(prefix = '')` -> iterator
1212

13-
Supports a legacy iteration mode (just an object with a function `next`).
13+
Will throw if prefix is not a valid class name (unless the prefix is `-`). The exception will be thrown when the first value is being yielded.
1414

15-
Use `.` as a prefix if you want real css class names.
15+
Class names are generated without a leading `.`.
1616

17-
# Example
18-
19-
ES5:
17+
It uses generators, so you'll have to be using a version of node that supports this.
2018

21-
```js
22-
const cssNameGenerator = require('css-class-generator');
23-
var generator = cssNameGenerator('prefix-');
24-
generator.next() // { value: 'prefix-A', done: false }
25-
generator.next() // { value: 'prefix-B', done: false }
26-
```
27-
28-
ES6:
19+
# Example
2920

3021
```js
3122
const cssNameGenerator = require('css-class-generator');
3223
for (let value of cssNameGenerator()) {
3324
// 'A', 'B', ...
3425
}
26+
for (let value of cssNameGenerator('-')) {
27+
// '-A', '-B', ...
28+
}
29+
for (let value of cssNameGenerator('custom-namespace-')) {
30+
// You get the idea...
31+
}
3532
```

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ function* firstTwoChars() {
5151
yield* appendGenerator(firstCharacters, secondCharacters());
5252
}
5353

54+
const validIdent = /^-?[_a-z][_a-z0-9-]*$/i;
55+
5456
module.exports = function* cssNameGenerator(prefix) {
57+
if (prefix && prefix !== '-' && !validIdent.test(prefix)) {
58+
throw new Error(`Expected prefix (${prefix}) to be a valid css class name`);
59+
}
60+
5561
if (!prefix) {
5662
yield* firstCharacters();
5763
yield* firstTwoChars();

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ test('generate name with other prefix', t => {
3131
t.true(isValid, `Expected ${value} to be a valid css selector`);
3232
}
3333
});
34+
35+
test('allows hyphen for class name', t => {
36+
t.notThrows(() => { cssNameGenerator('-').next(); });
37+
});
38+
39+
test('throws for invalid class name', t => {
40+
t.throws(() => { cssNameGenerator('--').next(); });
41+
t.throws(() => { cssNameGenerator('0').next(); });
42+
});

0 commit comments

Comments
 (0)