Skip to content

Commit

Permalink
fix camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Sep 18, 2018
1 parent 06f7906 commit 4297217
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
7 changes: 5 additions & 2 deletions camel-case.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"use strict";
function camelCase (str) {
return str.replace(/[\w-]+/g, (s) => (
/^-?([a-z]+(?:-[a-z]+)+)$/.test(s)
? RegExp.$1.replace(
/^-?[a-z]+(?:-[a-z]+)+$/.test(s)
? s.replace(
/^-(ms|moz|khtml|epub|(\w+-?)*webkit)(?=-)/i,
"$1"
).replace(
/-\w/g,
s => (
s[1].toUpperCase()
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"check-coverage": true
},
"scripts": {
"test": "nyc mocha --no-timeouts",
"debug": "mocha --inspect --debug-brk --no-timeouts"
"mocha": "mocha --no-timeouts",
"test": "nyc npm run mocha",
"debug": "npm run mocha -- --inspect-brk"
},
"dependencies": {
"@babel/core": "^7.0.0"
Expand All @@ -52,12 +53,12 @@
"postcss-syntax": ">=0.33.0"
},
"devDependencies": {
"autoprefixer": "^9.1.1",
"autoprefixer": "^9.1.5",
"chai": "^4.1.2",
"codecov": "^3.0.4",
"json5": "^1.0.1",
"codecov": "^3.1.0",
"json5": "^2.0.1",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"nyc": "^13.0.1",
"postcss": "^7.0.2",
"postcss-parser-tests": "^6.3.0",
"postcss-safe-parser": "^4.0.1",
Expand Down
17 changes: 7 additions & 10 deletions test/camel-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ const camelCase = require("../camel-case");
const unCamelCase = require("../un-camel-case");

const data = {
borderTopLeftRadius: "border-top-left-radius",
backgroundImage: "background-image",
xwebkitAnimation: "-xwebkit-animation",
webkitAnimation: "-webkit-animation",
epubAnimation: "-epub-animation",
mozAnimation: "-moz-animation",
msAnimation: "-ms-animation",
oAnimation: "-o-animation",
xAnimation: "-x-animation",
OAnimation: "-o-animation",
XAnimation: "-x-animation",
webkitApp: "-webkit-app",
borderTopLeftRadius: "border-top-left-radius",
backgroundImage: "background-image",
onChange: "on-change",
OnChange: "-on-change",
zIndex: "z-index",
"::selection": "::selection",
"::mozSelection": "::-moz-selection",
"::mozSelection,::selection": "::-moz-selection,::selection",
Expand Down Expand Up @@ -50,12 +53,6 @@ describe("camelCase", () => {
});

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);
Expand Down
2 changes: 1 addition & 1 deletion test/non-style.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
const spawnSync = require("child_process").spawnSync;
const fs = require("fs");
const files = spawnSync("git", ["ls-files"], {encoding: "utf8"}).stdout.match(/^.+$/gm).filter(file => file.endsWith(".js"));
const files = spawnSync("git", ["ls-files"], { encoding: "utf8" }).stdout.match(/^.+$/gm).filter(file => file.endsWith(".js"));
const syntax = require("../");
const expect = require("chai").expect;

Expand Down
6 changes: 3 additions & 3 deletions un-camel-case.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";
function unCamelCase (str) {
return str.replace(/[\w-]+/g, (s) => (
/^[a-z]*(?:[A-Z][a-z]+)+$/.test(s)
/^[A-Z]?[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-"
/^(o|ms|moz|khtml|epub|(\w+-?)*webkit)(?=-)/i,
"-$1"
)
: s
));
Expand Down

0 comments on commit 4297217

Please sign in to comment.