Skip to content

Commit

Permalink
update testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Jul 26, 2018
1 parent 5fc32fb commit 8753689
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 11 deletions.
17 changes: 13 additions & 4 deletions camel-case.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
"use strict";
function camelCase (str) {
if (str.startsWith("--")) {
return str;
}
return str.replace(/(^|\s|\W)-(ms-)/g, "$1$2").replace(/-+(\w)/g, (s, char) => s.length > 2 ? s : char.toUpperCase());
return str.replace(/[\w-]+/g, (s) => (
/^-?[a-z]+(?:-[a-z]+)+$/.test(s)
? s.replace(/^-/, "")
.replace(
/-(\w)/g,
(s, char, index) => (
index
? char.toUpperCase()
: char
)
)
: s
));
}

module.exports = camelCase;
5 changes: 1 addition & 4 deletions object-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
const getTemplate = require("./get-template");
const ObjectLiteral = require("./object");
const camelCase = require("./camel-case");
const unCamelCase = require("./un-camel-case");
const Literal = require("./literal");
const postcss = require("postcss");

function forEach (arr, callback) {
arr && arr.forEach(callback);
}

function unCamelCase (str) {
return str.replace(/[A-Z]/g, (char) => "-" + char.toLowerCase()).replace(/(^|\b)ms-/, "$1-ms-");
}

const replaceProp = (fn) => (value) => (
value.replace(/(\(\s*)(.*?)(\s*:)/g, (s, prefix, prop, suffix) => (
prefix + fn(prop) + suffix
Expand Down
2 changes: 1 addition & 1 deletion object.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ObjectLiteral extends Container {
constructor (defaults) {
super(defaults);
this.type = "object";
if (!this.nodes) this.nodes = [];
this.nodes = [];
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
"postcss-syntax": ">=0.31.0"
},
"devDependencies": {
"autoprefixer": "^9.0.0",
"autoprefixer": "^9.0.1",
"chai": "^4.1.2",
"codecov": "^3.0.4",
"json5": "^1.0.1",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"postcss": "^7.0.0",
"postcss": "^7.0.1",
"postcss-parser-tests": "^6.3.0",
"postcss-safe-parser": "^4.0.1",
"postcss-syntax": ">=0.31.0"
Expand Down
71 changes: 71 additions & 0 deletions test/camel-case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"use strict";

const expect = require("chai").expect;
const camelCase = require("../camel-case");
const unCamelCase = require("../un-camel-case");

const data = {
xwebkitAnimation: "-xwebkit-animation",
webkitAnimation: "-webkit-animation",
epubAnimation: "-epub-animation",
mozAnimation: "-moz-animation",
msAnimation: "-ms-animation",
oAnimation: "-o-animation",
xAnimation: "-x-animation",
webkitApp: "-webkit-app",
borderTopLeftRadius: "border-top-left-radius",
backgroundImage: "background-image",
"::selection": "::selection",
"::mozSelection": "::-moz-selection",
"::mozSelection,::selection": "::-moz-selection,::selection",
"--margin-top": "--margin-top",
"margin--top": "margin--top",
"height: webkitCalc(2vh-20px);": "height: -webkit-calc(2vh-20px);",
"calc(2vh-20px)": "calc(2vh-20px)",
"calc(2vh--20px)": "calc(2vh--20px)",
};

const testCases = Object.keys(data).map(prop => {
return {
camel: prop,
unCamel: data[prop],
};
});

const symbols = Array.from("@*:;\n,(){} ");

describe("camelCase", () => {
testCases.forEach(testCase => {
it(`${testCase.unCamel} => ${testCase.camel}`, () => {
expect(camelCase(testCase.unCamel)).to.equal(testCase.camel);
});
});
describe("symbols", () => {
symbols.forEach(symbol => {
it(JSON.stringify(symbol), () => {
expect(camelCase(testCases.map(testCase => testCase.unCamel).join(symbol))).to.equal(testCases.map(testCase => testCase.camel).join(symbol));
});
});
});
});

describe("unCamelCase", () => {
it("onChange => on-change", () => {
expect(unCamelCase("onChange")).to.equal("on-change");
});
it("OnChange => -on-change", () => {
expect(unCamelCase("OnChange")).to.equal("-on-change");
});
testCases.forEach(testCase => {
it(`${testCase.camel} => ${testCase.unCamel}`, () => {
expect(unCamelCase(testCase.camel)).to.equal(testCase.unCamel);
});
});
describe("symbols", () => {
symbols.forEach(symbol => {
it(JSON.stringify(symbol), () => {
expect(unCamelCase(testCases.map(testCase => testCase.camel).join(symbol))).to.equal(testCases.map(testCase => testCase.unCamel).join(symbol));
});
});
});
});
1 change: 1 addition & 0 deletions test/css-in-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe("CSS in JS", () => {
});
`);
});

describe("objectify for css", () => {
cases.each((name, css) => {
if (name === "bom.css") return;
Expand Down
16 changes: 16 additions & 0 deletions un-camel-case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
function unCamelCase (str) {
return str.replace(/[\w-]+/g, (s) => (
/^[a-z]*(?:[A-Z][a-z]+)+$/.test(s)
? s.replace(
/[A-Z]/g,
s => "-" + s.toLowerCase()
).replace(
/^(\w|ms|moz|khtml|epub|\w*webkit)-/,
"-$1-"
)
: s
));
}

module.exports = unCamelCase;

0 comments on commit 8753689

Please sign in to comment.