Skip to content

Commit

Permalink
Arrays as class list source
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouni Kantola committed Feb 22, 2016
1 parent f932429 commit 763b075
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
52 changes: 28 additions & 24 deletions lib/css-class-names/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
function split(text) {
const re = /\.([A-z-_][\w-]*)[\s\r\n]*(?=[\.\:{])/g;
const matches = new Set();
module.exports = (() => {
"use strict";

function split(text) {
const re = /\.([A-z-_][\w-]*)[\s\r\n]*(?=[\.\:{])/g;
const matches = new Set();

var match;
while (match = re.exec(text))
matches.add(match[1].trim());
let match;
while (match = re.exec(text))
matches.add(match[1].trim());

return Array.from(matches);
}
return Array.from(matches);
}

function replace(text, source, target) {
var re = new RegExp(`\\.${source}(?=[\\s\\r\\n\\.{])`, "g");
const targetCssClass = target.startsWith(".") ? target : `.${target}`;
return text.replace(re, targetCssClass);
}
function replace(text, source, target) {
var re = new RegExp(`\\.${source}(?=[\\s\\r\\n\\.{])`, "g");
const targetCssClass = target.startsWith(".") ? target : `.${target}`;
return text.replace(re, targetCssClass);
}

function replaceAll(text, cssClassMap) {
return Object.keys(cssClassMap).reduce((processedText, sourceCssClass) => {
return replace(processedText, sourceCssClass, cssClassMap[sourceCssClass]);
}, text);
}

module.exports = {
split: split,
replace: replace,
replaceAll: replaceAll
};
function replaceAll(text, cssClassMap) {
return Object.keys(cssClassMap).reduce((processedText, sourceCssClass) => {
return replace(processedText, sourceCssClass, cssClassMap[sourceCssClass]);
}, text);
}

return {
split: split,
replace: replace,
replaceAll: replaceAll
}
})()
6 changes: 6 additions & 0 deletions lib/css-class-names/test/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ test("should identify class hierarchies", t => {
const text = ".b-e .b-e--m {} .b-e.m{}";
const expected = ["b-e", "b-e--m", "m"];
t.same(cssClasses.split(text), expected);
});

test("should accept arrays as source", t => {
const text = [".first-file-class-1 {} .first-file-class-2 {}", '.second-file-class-1 {} .second-file-class-2 {}'];
const expected = ["first-file-class-1", "first-file-class-2", "second-file-class-1", "second-file-class-2"];
t.same(cssClasses.split(text), expected);
});
7 changes: 2 additions & 5 deletions lib/ruin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ module.exports = (glob) => {
Promise.all(files.map(io.read)).then(files => {

// 1. Build source class list
const sourceCssClasses = files.map(f => f.content)
.map(cssClasses.split)
.reduce((allCssClasses, fileCssClasses) => {
return allCssClasses.concat(fileCssClasses);
}, []);
const content = files.map(file => file.content);
const sourceCssClasses = cssClasses.split(content);

// 2. Build target class list
const targetCssClasses = shortCssClasses(sourceCssClasses.length);
Expand Down

0 comments on commit 763b075

Please sign in to comment.