Skip to content

Commit 3485952

Browse files
authored
update testcase (stylelint#24)
1 parent 5fc32fb commit 3485952

File tree

7 files changed

+107
-14
lines changed

7 files changed

+107
-14
lines changed

camel-case.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"use strict";
22
function camelCase (str) {
3-
if (str.startsWith("--")) {
4-
return str;
5-
}
6-
return str.replace(/(^|\s|\W)-(ms-)/g, "$1$2").replace(/-+(\w)/g, (s, char) => s.length > 2 ? s : char.toUpperCase());
3+
return str.replace(/[\w-]+/g, (s) => (
4+
/^-?([a-z]+(?:-[a-z]+)+)$/.test(s)
5+
? RegExp.$1.replace(
6+
/-\w/g,
7+
s => (
8+
s[1].toUpperCase()
9+
)
10+
)
11+
: s
12+
));
713
}
814

915
module.exports = camelCase;

object-parser.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
const getTemplate = require("./get-template");
33
const ObjectLiteral = require("./object");
44
const camelCase = require("./camel-case");
5+
const unCamelCase = require("./un-camel-case");
56
const Literal = require("./literal");
67
const postcss = require("postcss");
78

89
function forEach (arr, callback) {
910
arr && arr.forEach(callback);
1011
}
1112

12-
function unCamelCase (str) {
13-
return str.replace(/[A-Z]/g, (char) => "-" + char.toLowerCase()).replace(/(^|\b)ms-/, "$1-ms-");
14-
}
15-
1613
const replaceProp = (fn) => (value) => (
1714
value.replace(/(\(\s*)(.*?)(\s*:)/g, (s, prefix, prop, suffix) => (
1815
prefix + fn(prop) + suffix
@@ -199,11 +196,13 @@ class objectParser {
199196
if (node.value.type === "ObjectExpression") {
200197
let rule;
201198
if (/^@(\S+)(\s*)(.*)$/.test(key.value)) {
199+
const name = RegExp.$1;
200+
const afterName = RegExp.$2;
202201
const params = RegExp.$3;
203202
const atRule = postcss.atRule({
204-
name: unCamelCase(RegExp.$1),
203+
name: unCamelCase(name),
205204
raws: {
206-
afterName: RegExp.$2,
205+
afterName: afterName,
207206
},
208207
nodes: [],
209208
});

object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ObjectLiteral extends Container {
1616
constructor (defaults) {
1717
super(defaults);
1818
this.type = "object";
19-
if (!this.nodes) this.nodes = [];
19+
this.nodes = [];
2020
}
2121
}
2222

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252
"postcss-syntax": ">=0.31.0"
5353
},
5454
"devDependencies": {
55-
"autoprefixer": "^9.0.0",
55+
"autoprefixer": "^9.0.1",
5656
"chai": "^4.1.2",
5757
"codecov": "^3.0.4",
5858
"json5": "^1.0.1",
5959
"mocha": "^5.2.0",
6060
"nyc": "^12.0.2",
61-
"postcss": "^7.0.0",
61+
"postcss": "^7.0.1",
6262
"postcss-parser-tests": "^6.3.0",
6363
"postcss-safe-parser": "^4.0.1",
6464
"postcss-syntax": ">=0.31.0"

test/camel-case.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"use strict";
2+
3+
const expect = require("chai").expect;
4+
const camelCase = require("../camel-case");
5+
const unCamelCase = require("../un-camel-case");
6+
7+
const data = {
8+
xwebkitAnimation: "-xwebkit-animation",
9+
webkitAnimation: "-webkit-animation",
10+
epubAnimation: "-epub-animation",
11+
mozAnimation: "-moz-animation",
12+
msAnimation: "-ms-animation",
13+
oAnimation: "-o-animation",
14+
xAnimation: "-x-animation",
15+
webkitApp: "-webkit-app",
16+
borderTopLeftRadius: "border-top-left-radius",
17+
backgroundImage: "background-image",
18+
"::selection": "::selection",
19+
"::mozSelection": "::-moz-selection",
20+
"::mozSelection,::selection": "::-moz-selection,::selection",
21+
"--margin-top": "--margin-top",
22+
"margin--top": "margin--top",
23+
"height: webkitCalc(2vh-20px);": "height: -webkit-calc(2vh-20px);",
24+
"calc(2vh-20px)": "calc(2vh-20px)",
25+
"calc(2vh--20px)": "calc(2vh--20px)",
26+
};
27+
28+
const testCases = Object.keys(data).map(prop => {
29+
return {
30+
camel: prop,
31+
unCamel: data[prop],
32+
};
33+
});
34+
35+
const symbols = Array.from("@*:;\n,(){} ");
36+
37+
describe("camelCase", () => {
38+
testCases.forEach(testCase => {
39+
it(`${testCase.unCamel} => ${testCase.camel}`, () => {
40+
expect(camelCase(testCase.unCamel)).to.equal(testCase.camel);
41+
});
42+
});
43+
describe("symbols", () => {
44+
symbols.forEach(symbol => {
45+
it(JSON.stringify(symbol), () => {
46+
expect(camelCase(testCases.map(testCase => testCase.unCamel).join(symbol))).to.equal(testCases.map(testCase => testCase.camel).join(symbol));
47+
});
48+
});
49+
});
50+
});
51+
52+
describe("unCamelCase", () => {
53+
it("onChange => on-change", () => {
54+
expect(unCamelCase("onChange")).to.equal("on-change");
55+
});
56+
it("OnChange => -on-change", () => {
57+
expect(unCamelCase("OnChange")).to.equal("-on-change");
58+
});
59+
testCases.forEach(testCase => {
60+
it(`${testCase.camel} => ${testCase.unCamel}`, () => {
61+
expect(unCamelCase(testCase.camel)).to.equal(testCase.unCamel);
62+
});
63+
});
64+
describe("symbols", () => {
65+
symbols.forEach(symbol => {
66+
it(JSON.stringify(symbol), () => {
67+
expect(unCamelCase(testCases.map(testCase => testCase.camel).join(symbol))).to.equal(testCases.map(testCase => testCase.unCamel).join(symbol));
68+
});
69+
});
70+
});
71+
});

test/css-in-js.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("CSS in JS", () => {
2727
const out = `
2828
import glm from 'glamorous';
2929
const Component1 = glm.a({
30-
"::WebkitInputPlaceholder": {
30+
"::webkitInputPlaceholder": {
3131
color: "gray",
3232
},
3333
"::placeholder": {
@@ -137,6 +137,7 @@ describe("CSS in JS", () => {
137137
});
138138
`);
139139
});
140+
140141
describe("objectify for css", () => {
141142
cases.each((name, css) => {
142143
if (name === "bom.css") return;

un-camel-case.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
function unCamelCase (str) {
3+
return str.replace(/[\w-]+/g, (s) => (
4+
/^[a-z]*(?:[A-Z][a-z]+)+$/.test(s)
5+
? s.replace(
6+
/[A-Z]/g,
7+
s => "-" + s.toLowerCase()
8+
).replace(
9+
/^(\w|ms|moz|khtml|epub|\w*webkit)-/,
10+
"-$1-"
11+
)
12+
: s
13+
));
14+
}
15+
16+
module.exports = unCamelCase;

0 commit comments

Comments
 (0)