Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Mar 14, 2013
0 parents commit f94cc3d
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db

# Node.js #
###########
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log

# Components #
##############

/build
/components
/vendors
*.orig
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test:
@node test

install:
@npm install

tests: test

.PHONY: test
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Move Media

Aggregate media queries and move it to the end of the file.

### API

var moveMedia = require('move-media')

var css = rework(inputCSS)
.use(moveMedia(sortFunction))
.toString()

By default, `sortFunction` just sorts the queries alphabetically:

function sortFunction(a, b) {
if (a < b) return 1;
if (a > b) return -1;
return 0
}

### Limitations

- Assumes media query order does not matter.
If it does, then use your own sort function.
- Declarations within each media query preserves order.
- In general, keep the order in mind!

### License

WTFPL
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function (sortFn) {
if (!sortFn) {
sortFn = function (a, b) {
if (a < b) return 1;
if (b > a) return -1;
return 0
}
}

return function (style) {
var rules = []
var media = {}

style.rules.forEach(function (rule) {
var query = rule.media
if (!query) return rules.push(rule);

;[].push.apply((media[query] || (media[query] = {
media: query,
rules: []
})).rules, rule.rules)
})

style.rules = rules.concat(Object.keys(media).sort(sortFn).map(function (query) {
return media[query]
}))
}
}
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "rework-move-media",
"description": "Move media to the end of the CSS file",
"version": "0.0.1",
"peerDependencies": {
"rework": "*"
}
}
33 changes: 33 additions & 0 deletions test/fixtures/general.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@media (min-width: 100px) {
body {
color: black;
}
}

body {
color: yellow;
}

@media (min-width: 100px) {
body {
color: green;
}
}

@media (min-width: 100px) {
body {
color: red;
}
}

@media (max-width: 300px) {
body {
color: orange;
}
}

@media (min-width: 100px) {
body {
color: purple;
}
}
27 changes: 27 additions & 0 deletions test/fixtures/general.out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
body {
color: yellow
}

@media (min-width: 100px) {
body {
color: black
}

body {
color: green
}

body {
color: red
}

body {
color: purple
}
}

@media (max-width: 300px) {
body {
color: orange
}
}
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var fs = require('fs')
var assert = require('assert')

var rework = require('rework')
var inherit = require('../')

function read(file) {
return fs.readFileSync('test/fixtures/' + file + '.css', 'utf8')
}

function test(file, msg) {
var out = rework(read(file)).use(inherit()).toString()
assert.equal(out, read(file + '.out'), msg + ': ' + out)
}

test('general', 'General failed')

0 comments on commit f94cc3d

Please sign in to comment.