Skip to content

Commit a980df4

Browse files
refactor: replace cssToJs with style-to-js & remove camelCase
Save `style-to-js@1.1.0` to package.json "dependencies" https://github.com/remarkablemark/style-to-js/releases/tag/v1.1.0 Update tests
1 parent 8ea17fa commit a980df4

File tree

4 files changed

+5
-87
lines changed

4 files changed

+5
-87
lines changed

lib/attributes-to-props.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
var reactProperty = require('react-property');
2-
var styleToObject = require('style-to-object');
2+
var styleToJS = require('style-to-js').default;
33
var utilities = require('./utilities');
44

5-
var camelCase = utilities.camelCase;
6-
75
var htmlProperties = reactProperty.html;
86
var svgProperties = reactProperty.svg;
97
var isCustomAttribute = reactProperty.isCustomAttribute;
108

119
var hasOwnProperty = Object.prototype.hasOwnProperty;
1210

11+
var styleToJSOptions = { reactCompat: true };
12+
1313
/**
1414
* Converts HTML/SVG DOM attributes to React props.
1515
*
@@ -61,31 +61,10 @@ function attributesToProps(attributes) {
6161

6262
// convert inline style to object
6363
if (attributes.style != null) {
64-
props.style = cssToJs(attributes.style);
64+
props.style = styleToJS(attributes.style, styleToJSOptions);
6565
}
6666

6767
return props;
6868
}
6969

70-
/**
71-
* Converts inline CSS style to POJO (Plain Old JavaScript Object).
72-
*
73-
* @param {String} style - The inline CSS style.
74-
* @return {Object} - The style object.
75-
*/
76-
function cssToJs(style) {
77-
var styleObject = {};
78-
79-
if (style) {
80-
styleToObject(style, function (property, value) {
81-
// skip CSS comment
82-
if (property && value) {
83-
styleObject[camelCase(property)] = value;
84-
}
85-
});
86-
}
87-
88-
return styleObject;
89-
}
90-
9170
module.exports = attributesToProps;

lib/utilities.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
var React = require('react');
2-
var hyphenPatternRegex = /-([a-z])/g;
3-
var CUSTOM_PROPERTY_OR_NO_HYPHEN_REGEX = /^--[a-zA-Z0-9-]+$|^[^-]+$/;
4-
5-
/**
6-
* Converts a string to camelCase.
7-
*
8-
* @param {String} string - The string.
9-
* @return {String}
10-
*/
11-
function camelCase(string) {
12-
if (typeof string !== 'string') {
13-
throw new TypeError('First argument must be a string');
14-
}
15-
16-
// custom property or no hyphen found
17-
if (CUSTOM_PROPERTY_OR_NO_HYPHEN_REGEX.test(string)) {
18-
return string;
19-
}
20-
21-
// convert to camelCase
22-
return string
23-
.toLowerCase()
24-
.replace(hyphenPatternRegex, function (_, character) {
25-
return character.toUpperCase();
26-
});
27-
}
282

293
/**
304
* Swap key with value in an object.
@@ -104,7 +78,6 @@ var PRESERVE_CUSTOM_ATTRIBUTES = React.version.split('.')[0] >= 16;
10478

10579
module.exports = {
10680
PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
107-
camelCase: camelCase,
10881
invertObject: invertObject,
10982
isCustomComponent: isCustomComponent
11083
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@types/htmlparser2": "3.10.1",
3636
"html-dom-parser": "0.3.0",
3737
"react-property": "1.0.1",
38-
"style-to-object": "0.3.0"
38+
"style-to-js": "1.1.0"
3939
},
4040
"devDependencies": {
4141
"@commitlint/cli": "^8.3.5",

test/utilities.test.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,11 @@
11
const React = require('react');
22
const {
33
PRESERVE_CUSTOM_ATTRIBUTES,
4-
camelCase,
54
invertObject,
65
isCustomComponent
76
} = require('../lib/utilities');
87

98
describe('utilities', () => {
10-
describe('camelCase', () => {
11-
it.each([undefined, null, {}, [], 0, 1, () => {}, new Date()])(
12-
'throws an error if first argument is %p',
13-
input => {
14-
expect(() => {
15-
camelCase(input);
16-
}).toThrow(TypeError);
17-
}
18-
);
19-
20-
it.each([
21-
['', ''],
22-
['foo', 'foo'],
23-
['fooBar', 'fooBar'],
24-
['--fooBar', '--fooBar'],
25-
['--foo-bar', '--foo-bar'],
26-
['--foo-100', '--foo-100']
27-
])(
28-
'does not modify string if it does not need to be camelCased',
29-
(input, expected) => {
30-
expect(camelCase(input)).toBe(expected);
31-
}
32-
);
33-
34-
it.each([
35-
['foo-bar', 'fooBar'],
36-
['foo-bar-baz', 'fooBarBaz'],
37-
['CAMEL-CASE', 'camelCase']
38-
])('camelCases a string', (input, expected) => {
39-
expect(camelCase(input)).toBe(expected);
40-
});
41-
});
42-
439
describe('invertObject', () => {
4410
it.each([undefined, null, 'string', 0, 1, () => {}])(
4511
`throws an error if the first argument is %p`,

0 commit comments

Comments
 (0)