Skip to content
This repository was archived by the owner on Mar 28, 2018. It is now read-only.

Commit e461af7

Browse files
committed
Implement sort by cp-ing @jxnblk's code
0 parents  commit e461af7

File tree

8 files changed

+159
-0
lines changed

8 files changed

+159
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- 'stable'
4+
- '5'
5+
- '4'

index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict'
2+
3+
// Originally written by @jxnblk
4+
// https://github.com/cssstats/cssstats/blob/16fe37c96623fd3cdb24bd302ef6f6e93826af0f/controllers/stats.js#L98
5+
module.exports = units => units.sort(sortFn)
6+
7+
const sortFn = (a, b) => {
8+
const aa = convert(a)
9+
const bb = convert(b)
10+
11+
if (aa === bb) return 0
12+
return aa < bb ? 1 : -1
13+
}
14+
15+
const convert = value => {
16+
let raw
17+
18+
if (typeof value !== 'string') {
19+
value = value.toString()
20+
}
21+
22+
raw = parseFloat(value, 10)
23+
24+
if (value.match(/px$/)) return raw
25+
if (value.match(/em$/)) return raw * 16
26+
if (value.match(/%$/)) return raw * .16
27+
28+
switch (value) {
29+
case 'inherit':
30+
return 16
31+
case 'xx-small':
32+
return 9
33+
case 'x-small':
34+
return 10
35+
case 'small':
36+
return 13
37+
case 'medium':
38+
return 16
39+
case 'large':
40+
return 18
41+
case 'x-large':
42+
return 24
43+
case 'xx-large':
44+
return 32
45+
case 'small':
46+
return 13
47+
case 'larger':
48+
return 19
49+
default:
50+
return 1024
51+
}
52+
}

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 John Otander
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "css-unit-sort",
3+
"description": "Sort css values no matter the unit",
4+
"author": "John Otander",
5+
"version": "0.1.0",
6+
"main": "index.js",
7+
"files": [
8+
"index.js"
9+
],
10+
"scripts": {
11+
"test": "ava -v"
12+
},
13+
"repository": "cssstats/css-unit-sort",
14+
"keywords": [],
15+
"license": "MIT",
16+
"dependencies": {},
17+
"devDependencies": {
18+
"ava": "*"
19+
}
20+
}

readme.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# css-unit-sort [![Build Status](https://secure.travis-ci.org/cssstats/css-unit-sort.svg?branch=master)](https://travis-ci.org/cssstats/css-unit-sort) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
2+
3+
Sort css values no matter the unit.
4+
Originally [written](https://github.com/cssstats/cssstats/blob/16fe37c96623fd3cdb24bd302ef6f6e93826af0f/controllers/stats.js#L98) by [@jxnblk](https://twitter.com/jxnblk).
5+
6+
## Installation
7+
8+
```bash
9+
npm i -S css-unit-sort
10+
```
11+
12+
## Usage
13+
14+
```javascript
15+
const cssUnitSort = require('css-unit-sort')
16+
17+
cssUnitSort([
18+
'2rem', 'inherit', 'small', '12px', '20px', '60px', '1.5em'
19+
]) // => ['60px', '2rem', '1.5em', '20px', 'inherit', 'small', '12px']
20+
```
21+
22+
## License
23+
24+
MIT
25+
26+
## Contributing
27+
28+
1. Fork it
29+
2. Create your feature branch (`git checkout -b my-new-feature`)
30+
3. Commit your changes (`git commit -am 'Add some feature'`)
31+
4. Push to the branch (`git push origin my-new-feature`)
32+
5. Create new Pull Request
33+
34+
Crafted with <3 by John Otander ([@4lpine](https://twitter.com/4lpine)).
35+
36+
***
37+
38+
> This package was initially generated with [yeoman](http://yeoman.io) and the [p generator](https://github.com/johnotander/generator-p.git).

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
import cssUnitSort from './'
3+
4+
test('sorts units', t => {
5+
t.deepEqual(cssUnitSort([
6+
'2rem', 'inherit', 'small', '12px', '20px', '60px', '1.5em'
7+
]), [
8+
'60px', '2rem', '1.5em', '20px', 'inherit', 'small', '12px'
9+
])
10+
})

0 commit comments

Comments
 (0)