Skip to content

Commit 638c4da

Browse files
author
Alex Zherdev
committed
Fix crash in case conversion helpers.
Resolves #118.
1 parent 268931b commit 638c4da

File tree

2 files changed

+72
-43
lines changed

2 files changed

+72
-43
lines changed

Diff for: lib/helpers.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,38 @@ const isPlainObject = (val) => !!val && typeof val === 'object' && val.construct
6060
const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
6161

6262
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/toKebabCase.md
63-
const toKebabCase = (str) =>
64-
str &&
65-
str
66-
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
67-
.map((x) => x.toLowerCase())
68-
.join('-');
63+
const toKebabCase = (str) => {
64+
const match =
65+
str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g);
6966

67+
if (!match) {
68+
return str;
69+
}
70+
71+
return match.map((x) => x.toLowerCase()).join('-');
72+
};
7073
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/toSnakeCase.md
71-
const toSnakeCase = (str) =>
72-
str &&
73-
str
74-
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
75-
.map((x) => x.toLowerCase())
76-
.join('_');
74+
const toSnakeCase = (str) => {
75+
const match =
76+
str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g);
77+
78+
if (!match) {
79+
return str;
80+
}
81+
82+
return match.map((x) => x.toLowerCase()).join('_');
83+
};
7784

7885
// https://github.com/30-seconds/30-seconds-of-code/blob/master/snippets/toCamelCase.md
7986
const toCamelCase = (str) => {
80-
const s =
81-
str &&
82-
str
83-
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
84-
.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
85-
.join('');
87+
const match =
88+
str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g);
89+
90+
if (!match) {
91+
return str;
92+
}
93+
94+
const s = match.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()).join('');
8695
return s.slice(0, 1).toLowerCase() + s.slice(1);
8796
};
8897

Diff for: test/unit/helpers.test.js

+45-25
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
11
const { expect } = require('chai');
22

3-
const { set } = require('../../lib/helpers');
3+
const { set, toCamelCase, toSnakeCase, toKebabCase } = require('../../lib/helpers');
44

55
describe('Helpers', () => {
6-
it('should do nothing if it is not an object', () => {
7-
const object = 1;
8-
set(object, 'exist', true);
9-
expect(object).to.equal(1);
10-
});
6+
describe('set', () => {
7+
it('should do nothing if it is not an object', () => {
8+
const object = 1;
9+
set(object, 'exist', true);
10+
expect(object).to.equal(1);
11+
});
1112

12-
it('should set a value by path', () => {
13-
const object = { exist: {} };
14-
set(object, 'exist', true);
15-
set(object, 'a[0].b.c', 4);
16-
set(object, ['x', '0', 'y', 'z'], 5);
17-
expect(object).to.deep.equal({
18-
exist: true,
19-
a: [
20-
{
21-
b: {
22-
c: 4,
13+
it('should set a value by path', () => {
14+
const object = { exist: {} };
15+
set(object, 'exist', true);
16+
set(object, 'a[0].b.c', 4);
17+
set(object, ['x', '0', 'y', 'z'], 5);
18+
expect(object).to.deep.equal({
19+
exist: true,
20+
a: [
21+
{
22+
b: {
23+
c: 4,
24+
},
2325
},
24-
},
25-
],
26-
x: [
27-
{
28-
y: {
29-
z: 5,
26+
],
27+
x: [
28+
{
29+
y: {
30+
z: 5,
31+
},
3032
},
31-
},
32-
],
33+
],
34+
});
35+
});
36+
});
37+
38+
describe('toCamelCase', () => {
39+
it('should not choke on non-alpha characters', () => {
40+
expect(toCamelCase('*')).to.equal('*');
41+
});
42+
});
43+
44+
describe('toSnakeCase', () => {
45+
it('should not choke on non-alphanumeric characters', () => {
46+
expect(toSnakeCase('*')).to.equal('*');
47+
});
48+
});
49+
50+
describe('toKebabCase', () => {
51+
it('should not choke on non-alphanumeric characters', () => {
52+
expect(toKebabCase('*')).to.equal('*');
3353
});
3454
});
3555
});

0 commit comments

Comments
 (0)