Skip to content

Instantly share code, notes, and snippets.

@crongro
Forked from adhrinae/cssMinifier.js
Last active February 20, 2018 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crongro/1ddfc2e6ddc875a4765dea3bee26b984 to your computer and use it in GitHub Desktop.
Save crongro/1ddfc2e6ddc875a4765dea3bee26b984 to your computer and use it in GitHub Desktop.
const fs = require('fs');
//functional util
const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
const reduceByFun = (data) => (fun) => (initValue) => data.reduce((p,n) => fun(p,n), initValue);
//string utils
const trim = (s) => s.trim(s);
const removeNewLine = (s) => s.replace(/[\r\n]/g, '');
//main
fs.readFile('./target.css', { encoding: 'utf-8' }, (err, str) => {
const gubuns = [',', ':', ';', '{', '}'];
const result = CSSMinifier.run(str, gubuns);
console.log(result);
});
//CSSMinifier Object
const CSSMinifier = {
run(str, gubuns) {
const removeSpaces = reduceByFun(gubuns)(this.removeSpaceAroudChar);
const pipeSet = [removeNewLine, trim, removeSpaces];
return this.executePipe(str, pipeSet);
},
removeSpaceAroudChar(s, gubun) {
const reg = new RegExp(`\\s*${gubun}\\s*`, 'g');
return s.replace(reg, `${gubun}`);
},
executePipe(str, pipeSet) {
const result = pipe(...pipeSet)(str)
return result;
}
}
/* page-setup.css */
html {
font-size: 62.5%;
box-sizing: border-box;
}
html, body {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
}
div, p, h1, h2, h3, h4, h5, h6, ul, ol, li, dl, dt, dd,
table, th, td, form, fieldset, legend, input, textarea, blockquote, button {
margin:0 auto;
padding:0;
border:1px solid red;
}
*, *:before, *:after {
box-sizing: inherit;
}
li {
list-style: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment