aesthetic-utils
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -0,1 +1,11 @@ | ||
## 3.1.0 - 2020-01-26 | ||
#### 🚀 Updates | ||
- Migrated to Rollup for a smaller filesize. | ||
#### 📦 Dependencies | ||
- Updated all to latest. | ||
# 3.0.0 - 2019-12-19 | ||
@@ -2,0 +12,0 @@ |
229
esm/index.js
@@ -1,16 +0,213 @@ | ||
/** | ||
* @copyright 2017-2019, Miles Johnson | ||
* @license https://opensource.org/licenses/MIT | ||
*/ | ||
import formatFontFace from './formatFontFace'; | ||
import getFlushedStyles from './getFlushedStyles'; | ||
import getStyleElements from './getStyleElements'; | ||
import hasQueryCondition from './hasQueryCondition'; | ||
import isObject from './isObject'; | ||
import isRTL from './isRTL'; | ||
import purgeStyles from './purgeStyles'; | ||
import stripClassPrefix from './stripClassPrefix'; | ||
import toArray from './toArray'; | ||
import toObjectRecursive from './toObjectRecursive'; | ||
export * from './constants'; | ||
export { formatFontFace, getFlushedStyles, getStyleElements, hasQueryCondition, isObject, isRTL, purgeStyles, stripClassPrefix, toArray, toObjectRecursive }; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function toArray(value) { | ||
if (!value) { | ||
return []; | ||
} | ||
return Array.isArray(value) ? value : [value]; | ||
} | ||
var FORMATS = { | ||
'.eot': 'embedded-opentype', | ||
'.otf': 'opentype', | ||
'.svg': 'svg', | ||
'.svgz': 'svg', | ||
'.ttf': 'truetype', | ||
'.woff': 'woff', | ||
'.woff2': 'woff2' | ||
}; | ||
function formatFontFace(properties) { | ||
var fontFace = _extends({}, properties); | ||
var src = []; | ||
if (fontFace.local) { | ||
toArray(fontFace.local).forEach(function (alias) { | ||
src.push("local('" + String(alias) + "')"); | ||
}); | ||
delete fontFace.local; | ||
} | ||
if (Array.isArray(fontFace.srcPaths)) { | ||
toArray(fontFace.srcPaths).forEach(function (srcPath) { | ||
var ext = srcPath.slice(srcPath.lastIndexOf('.')); | ||
if (ext.includes('?')) { | ||
var _ext$split = ext.split('?'); | ||
ext = _ext$split[0]; | ||
} | ||
if (FORMATS[ext]) { | ||
src.push("url('" + srcPath + "') format('" + FORMATS[ext] + "')"); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
throw new Error("Unsupported font format \"" + ext + "\"."); | ||
} | ||
}); | ||
delete fontFace.srcPaths; | ||
} else { | ||
return fontFace; | ||
} | ||
fontFace.src = src.join(', '); | ||
return fontFace; | ||
} | ||
var QUERY_GROUP = /\([\x2Da-z\u017F\u212A]+:/gi; | ||
function hasQueryCondition(value) { | ||
return !!value.match(QUERY_GROUP); | ||
} | ||
function getFlushedStyles(styles) { | ||
return toArray(styles).reduce(function (css, style) { | ||
var sheet = style.sheet; | ||
var content = ''; | ||
if (sheet && sheet.cssRules) { | ||
content = Array.from(sheet.cssRules).map(function (rule) { | ||
return rule ? rule.cssText : ''; | ||
}).join('\n'); | ||
} else if (style.textContent) { | ||
content = style.textContent; | ||
} | ||
if (!content) { | ||
return css; | ||
} | ||
if (style.media && hasQueryCondition(style.media)) { | ||
content = "@media " + style.media + " { " + content + " }"; | ||
} | ||
return (css + "\n" + content).trim(); | ||
}, ''); | ||
} | ||
function getStyleElements(namespace) { | ||
return Array.from(document.querySelectorAll(namespace ? "style[" + namespace + "]" : 'style')); | ||
} | ||
function isObject(value) { | ||
return value !== null && !Array.isArray(value) && typeof value === 'object'; | ||
} | ||
function isRTL(dir) { | ||
return !!dir && dir === 'rtl'; | ||
} | ||
var NON_GLOBAL_PREFIX = /^(#|\.|@)/; | ||
function containsNestedRules(rule) { | ||
return typeof CSSStyleSheet !== 'undefined' && rule instanceof CSSStyleSheet || typeof CSSMediaRule !== 'undefined' && rule instanceof CSSMediaRule || typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule; | ||
} | ||
function deleteRule(parent, ruleToDelete) { | ||
var filterList = false; | ||
Array.from(parent.cssRules).some(function (rule, index) { | ||
if (rule === ruleToDelete && parent.cssRules[index]) { | ||
if (typeof parent.deleteRule === 'function') { | ||
parent.deleteRule(index); | ||
} else { | ||
filterList = true; | ||
delete parent.cssRules[index]; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (filterList || process.env.NODE_ENV === 'test') { | ||
parent.cssRules = parent.cssRules.filter(Boolean); | ||
} | ||
if (parent.cssRules.length === 0 && parent instanceof CSSRule) { | ||
if (containsNestedRules(parent.parentRule)) { | ||
deleteRule(parent.parentRule, parent); | ||
} | ||
if (containsNestedRules(parent.parentStyleSheet)) { | ||
deleteRule(parent.parentStyleSheet, parent); | ||
} | ||
} | ||
} | ||
function purgeRules(parent, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
if (!parent.cssRules) { | ||
return; | ||
} | ||
var rulesToDelete = []; | ||
Array.from(parent.cssRules).forEach(function (rule) { | ||
if (containsNestedRules(rule)) { | ||
purgeRules(rule); | ||
} | ||
if (onlyGlobals && NON_GLOBAL_PREFIX.test(rule.cssText)) { | ||
return; | ||
} | ||
rulesToDelete.push(rule); | ||
}); | ||
rulesToDelete.forEach(function (rule) { | ||
deleteRule(parent, rule); | ||
}); | ||
} | ||
function purgeStyles(styles, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
toArray(styles).forEach(function (style) { | ||
style.textContent = ''; | ||
if (containsNestedRules(style.sheet)) { | ||
purgeRules(style.sheet, onlyGlobals); | ||
} | ||
}); | ||
} | ||
function stripClassPrefix(name) { | ||
return name.charAt(0) === '.' ? name.slice(1) : name; | ||
} | ||
function toObjectRecursive(map) { | ||
var object = {}; | ||
if (map instanceof Map) { | ||
map.forEach(function (obj, key) { | ||
object[key] = obj.toObject(); | ||
}); | ||
} else { | ||
Object.keys(map).forEach(function (key) { | ||
object[key] = map[key].toObject(); | ||
}); | ||
} | ||
return object; | ||
} | ||
export { NON_GLOBAL_PREFIX, formatFontFace, getFlushedStyles, getStyleElements, hasQueryCondition, isObject, isRTL, purgeStyles, stripClassPrefix, toArray, toObjectRecursive }; |
242
lib/index.js
@@ -1,65 +0,227 @@ | ||
"use strict"; | ||
'use strict'; | ||
exports.__esModule = true; | ||
var _exportNames = { | ||
formatFontFace: true, | ||
getFlushedStyles: true, | ||
getStyleElements: true, | ||
hasQueryCondition: true, | ||
isObject: true, | ||
isRTL: true, | ||
purgeStyles: true, | ||
stripClassPrefix: true, | ||
toArray: true, | ||
toObjectRecursive: true | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function toArray(value) { | ||
if (!value) { | ||
return []; | ||
} | ||
return Array.isArray(value) ? value : [value]; | ||
} | ||
var FORMATS = { | ||
'.eot': 'embedded-opentype', | ||
'.otf': 'opentype', | ||
'.svg': 'svg', | ||
'.svgz': 'svg', | ||
'.ttf': 'truetype', | ||
'.woff': 'woff', | ||
'.woff2': 'woff2' | ||
}; | ||
function formatFontFace(properties) { | ||
var fontFace = _extends({}, properties); | ||
var _formatFontFace = _interopRequireDefault(require("./formatFontFace")); | ||
var src = []; | ||
exports.formatFontFace = _formatFontFace.default; | ||
if (fontFace.local) { | ||
toArray(fontFace.local).forEach(function (alias) { | ||
src.push("local('" + String(alias) + "')"); | ||
}); | ||
delete fontFace.local; | ||
} | ||
var _getFlushedStyles = _interopRequireDefault(require("./getFlushedStyles")); | ||
if (Array.isArray(fontFace.srcPaths)) { | ||
toArray(fontFace.srcPaths).forEach(function (srcPath) { | ||
var ext = srcPath.slice(srcPath.lastIndexOf('.')); | ||
exports.getFlushedStyles = _getFlushedStyles.default; | ||
if (ext.includes('?')) { | ||
var _ext$split = ext.split('?'); | ||
var _getStyleElements = _interopRequireDefault(require("./getStyleElements")); | ||
ext = _ext$split[0]; | ||
} | ||
exports.getStyleElements = _getStyleElements.default; | ||
if (FORMATS[ext]) { | ||
src.push("url('" + srcPath + "') format('" + FORMATS[ext] + "')"); | ||
} else if ("production" !== process.env.NODE_ENV) { | ||
throw new Error("Unsupported font format \"" + ext + "\"."); | ||
} | ||
}); | ||
delete fontFace.srcPaths; | ||
} else { | ||
return fontFace; | ||
} | ||
var _hasQueryCondition = _interopRequireDefault(require("./hasQueryCondition")); | ||
fontFace.src = src.join(', '); | ||
return fontFace; | ||
} | ||
exports.hasQueryCondition = _hasQueryCondition.default; | ||
var QUERY_GROUP = /\([\x2Da-z\u017F\u212A]+:/gi; | ||
function hasQueryCondition(value) { | ||
return !!value.match(QUERY_GROUP); | ||
} | ||
var _isObject = _interopRequireDefault(require("./isObject")); | ||
function getFlushedStyles(styles) { | ||
return toArray(styles).reduce(function (css, style) { | ||
var sheet = style.sheet; | ||
var content = ''; | ||
exports.isObject = _isObject.default; | ||
if (sheet && sheet.cssRules) { | ||
content = Array.from(sheet.cssRules).map(function (rule) { | ||
return rule ? rule.cssText : ''; | ||
}).join('\n'); | ||
} else if (style.textContent) { | ||
content = style.textContent; | ||
} | ||
var _isRTL = _interopRequireDefault(require("./isRTL")); | ||
if (!content) { | ||
return css; | ||
} | ||
exports.isRTL = _isRTL.default; | ||
if (style.media && hasQueryCondition(style.media)) { | ||
content = "@media " + style.media + " { " + content + " }"; | ||
} | ||
var _purgeStyles = _interopRequireDefault(require("./purgeStyles")); | ||
return (css + "\n" + content).trim(); | ||
}, ''); | ||
} | ||
exports.purgeStyles = _purgeStyles.default; | ||
function getStyleElements(namespace) { | ||
return Array.from(document.querySelectorAll(namespace ? "style[" + namespace + "]" : 'style')); | ||
} | ||
var _stripClassPrefix = _interopRequireDefault(require("./stripClassPrefix")); | ||
function isObject(value) { | ||
return value !== null && !Array.isArray(value) && typeof value === 'object'; | ||
} | ||
exports.stripClassPrefix = _stripClassPrefix.default; | ||
function isRTL(dir) { | ||
return !!dir && dir === 'rtl'; | ||
} | ||
var _toArray = _interopRequireDefault(require("./toArray")); | ||
var NON_GLOBAL_PREFIX = /^(#|\.|@)/; | ||
exports.toArray = _toArray.default; | ||
function containsNestedRules(rule) { | ||
return typeof CSSStyleSheet !== 'undefined' && rule instanceof CSSStyleSheet || typeof CSSMediaRule !== 'undefined' && rule instanceof CSSMediaRule || typeof CSSSupportsRule !== 'undefined' && rule instanceof CSSSupportsRule; | ||
} | ||
var _toObjectRecursive = _interopRequireDefault(require("./toObjectRecursive")); | ||
function deleteRule(parent, ruleToDelete) { | ||
var filterList = false; | ||
Array.from(parent.cssRules).some(function (rule, index) { | ||
if (rule === ruleToDelete && parent.cssRules[index]) { | ||
if (typeof parent.deleteRule === 'function') { | ||
parent.deleteRule(index); | ||
} else { | ||
filterList = true; | ||
delete parent.cssRules[index]; | ||
} | ||
exports.toObjectRecursive = _toObjectRecursive.default; | ||
return true; | ||
} | ||
var _constants = require("./constants"); | ||
return false; | ||
}); | ||
Object.keys(_constants).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
exports[key] = _constants[key]; | ||
}); | ||
if (filterList || process.env.NODE_ENV === 'test') { | ||
parent.cssRules = parent.cssRules.filter(Boolean); | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
if (parent.cssRules.length === 0 && parent instanceof CSSRule) { | ||
if (containsNestedRules(parent.parentRule)) { | ||
deleteRule(parent.parentRule, parent); | ||
} | ||
if (containsNestedRules(parent.parentStyleSheet)) { | ||
deleteRule(parent.parentStyleSheet, parent); | ||
} | ||
} | ||
} | ||
function purgeRules(parent, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
if (!parent.cssRules) { | ||
return; | ||
} | ||
var rulesToDelete = []; | ||
Array.from(parent.cssRules).forEach(function (rule) { | ||
if (containsNestedRules(rule)) { | ||
purgeRules(rule); | ||
} | ||
if (onlyGlobals && NON_GLOBAL_PREFIX.test(rule.cssText)) { | ||
return; | ||
} | ||
rulesToDelete.push(rule); | ||
}); | ||
rulesToDelete.forEach(function (rule) { | ||
deleteRule(parent, rule); | ||
}); | ||
} | ||
function purgeStyles(styles, onlyGlobals) { | ||
if (onlyGlobals === void 0) { | ||
onlyGlobals = false; | ||
} | ||
toArray(styles).forEach(function (style) { | ||
style.textContent = ''; | ||
if (containsNestedRules(style.sheet)) { | ||
purgeRules(style.sheet, onlyGlobals); | ||
} | ||
}); | ||
} | ||
function stripClassPrefix(name) { | ||
return name.charAt(0) === '.' ? name.slice(1) : name; | ||
} | ||
function toObjectRecursive(map) { | ||
var object = {}; | ||
if (map instanceof Map) { | ||
map.forEach(function (obj, key) { | ||
object[key] = obj.toObject(); | ||
}); | ||
} else { | ||
Object.keys(map).forEach(function (key) { | ||
object[key] = map[key].toObject(); | ||
}); | ||
} | ||
return object; | ||
} | ||
exports.NON_GLOBAL_PREFIX = NON_GLOBAL_PREFIX; | ||
exports.formatFontFace = formatFontFace; | ||
exports.getFlushedStyles = getFlushedStyles; | ||
exports.getStyleElements = getStyleElements; | ||
exports.hasQueryCondition = hasQueryCondition; | ||
exports.isObject = isObject; | ||
exports.isRTL = isRTL; | ||
exports.purgeStyles = purgeStyles; | ||
exports.stripClassPrefix = stripClassPrefix; | ||
exports.toArray = toArray; | ||
exports.toObjectRecursive = toObjectRecursive; |
{ | ||
"name": "aesthetic-utils", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Utility functions for Aesthetic.", | ||
@@ -10,3 +10,3 @@ "keywords": [ | ||
], | ||
"repository": "https://github.com/milesj/aesthetic/tree/master/packages/utils", | ||
"repository": "https://github.com/milesj/aesthetic/tree/master/packages/utils-legacy", | ||
"license": "MIT", | ||
@@ -24,3 +24,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "e2e68e0ad88fac9e8e9b7d04643fc65592af9a41" | ||
"gitHead": "464508115172da900d3d14ea1a5b255c982649f8" | ||
} |
@@ -5,3 +5,3 @@ # Aesthetic Utilities | ||
[![npm version](https://badge.fury.io/js/aesthetic-utils.svg)](https://www.npmjs.com/package/aesthetic-utils) | ||
[![npm deps](https://david-dm.org/milesj/aesthetic.svg?path=packages/utils)](https://www.npmjs.com/package/aesthetic-utils) | ||
[![npm deps](https://david-dm.org/milesj/aesthetic.svg?path=packages/utils-legacy)](https://www.npmjs.com/package/aesthetic-utils) | ||
@@ -8,0 +8,0 @@ Utility functions for supporting [Aesthetic](https://github.com/milesj/aesthetic) unified syntax and |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
1
18588
30
391
1