css-declaration-sorter
Advanced tools
Comparing version 1.7.1 to 2.0.0
{ | ||
"name": "css-declaration-sorter", | ||
"version": "1.7.1", | ||
"version": "2.0.0", | ||
"description": "Sorts CSS declarations fast and automatically in a certain order.", | ||
@@ -35,6 +35,7 @@ "keywords": [ | ||
"preversion": "npm test", | ||
"postversion": "git push && npm publish", | ||
"postversion": "git push && git push --tags && npm publish", | ||
"lint": "eslint src/*.js bin/*.js tests/*.js", | ||
"watch": "watch 'npm run test && npm run lint --silent' src bin tests", | ||
"scrape": "node src/property-scraper" | ||
"scrape": "node src/property-scraper", | ||
"ci": "npm test && npm run lint -- --max-warnings 0" | ||
}, | ||
@@ -41,0 +42,0 @@ "dependencies": { |
100
src/index.js
@@ -9,25 +9,29 @@ 'use strict'; | ||
// Sort CSS declarations alphabetically or using the set sorting order | ||
function sortCssDecls (cssDecls, sortOrder) { | ||
if (sortOrder === 'alphabetically') { | ||
timsort(cssDecls, function (a, b) { | ||
if (a.prop !== b.prop) { | ||
return a.prop < b.prop ? -1 : 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} else { | ||
timsort(cssDecls, function (a, b) { | ||
const aIndex = sortOrder.indexOf(a.prop); | ||
const bIndex = sortOrder.indexOf(b.prop); | ||
module.exports = postcss.plugin('css-declaration-sorter', function (options) { | ||
return function (css) { | ||
let sortOrderPath; | ||
if (aIndex !== bIndex) { | ||
return aIndex < bIndex ? -1 : 1; | ||
} else { | ||
return 0; | ||
} | ||
options = options || {}; | ||
// Use included sorting order if order is passed and not alphabetically | ||
if (options.order && options.order !== 'alphabetically') { | ||
sortOrderPath = path.join(__dirname, '../orders/', options.order) + '.json'; | ||
} else if (options.customOrder) { | ||
sortOrderPath = options.customOrder; | ||
} else { | ||
// Fallback to the default sorting order | ||
return processCss(css, 'alphabetically'); | ||
} | ||
// Load in the array containing the order from a JSON file | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(sortOrderPath, function (error, data) { | ||
if (error) return reject(error); | ||
resolve(data); | ||
}); | ||
}).then(function (data) { | ||
return processCss(css, JSON.parse(data)); | ||
}); | ||
} | ||
} | ||
}; | ||
}); | ||
@@ -73,3 +77,2 @@ function processCss (css, sortOrder) { | ||
} | ||
return; | ||
@@ -99,28 +102,31 @@ } | ||
module.exports = postcss.plugin('css-declaration-sorter', function (options) { | ||
return function (css) { | ||
let sortOrderPath; | ||
// Sort CSS declarations alphabetically or using the set sorting order | ||
function sortCssDecls (cssDecls, sortOrder) { | ||
if (sortOrder === 'alphabetically') { | ||
timsort(cssDecls, function (a, b) { | ||
if (a.type === 'decl' && b.type === 'decl') { | ||
return comparator(a.prop, b.prop); | ||
} else { | ||
return compareDifferentType(a, b); | ||
} | ||
}); | ||
} else { | ||
timsort(cssDecls, function (a, b) { | ||
if (a.type === 'decl' && b.type === 'decl') { | ||
const aIndex = sortOrder.indexOf(a.prop); | ||
const bIndex = sortOrder.indexOf(b.prop); | ||
return comparator(aIndex, bIndex); | ||
} else { | ||
return compareDifferentType(a, b); | ||
} | ||
}); | ||
} | ||
} | ||
options = options || {}; | ||
function comparator (a, b) { | ||
return a === b ? 0 : a < b ? -1 : 1; | ||
} | ||
// Use included sorting order if order is passed and not alphabetically | ||
if (options.order && options.order !== 'alphabetically') { | ||
sortOrderPath = path.join(__dirname, '../orders/', options.order) + '.json'; | ||
} else if (options.customOrder) { | ||
sortOrderPath = options.customOrder; | ||
} else { | ||
// Fallback to the default sorting order | ||
return processCss(css, 'alphabetically'); | ||
} | ||
// Load in the array containing the order from a JSON file | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(sortOrderPath, function (error, data) { | ||
if (error) return reject(error); | ||
resolve(data); | ||
}); | ||
}).then(function (data) { | ||
return processCss(css, JSON.parse(data)); | ||
}); | ||
}; | ||
}); | ||
function compareDifferentType (a, b) { | ||
return (a.type === 'decl') ? -1 : (b.type === 'decl') ? 1 : 0; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31861
1091