style-to-object
Advanced tools
Comparing version 0.4.4 to 1.0.0
@@ -11,4 +11,2 @@ (function (global, factory) { | ||
var styleToObject = {exports: {}}; | ||
// http://www.w3.org/TR/CSS21/grammar.html | ||
@@ -47,3 +45,3 @@ // https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027 | ||
*/ | ||
var inlineStyleParser = function(style, options) { | ||
var inlineStyleParser = function (style, options) { | ||
if (typeof style !== 'string') { | ||
@@ -82,3 +80,3 @@ throw new TypeError('First argument must be a string'); | ||
var start = { line: lineno, column: column }; | ||
return function(node) { | ||
return function (node) { | ||
node.position = new Position(start); | ||
@@ -274,3 +272,3 @@ whitespace(); | ||
var parse = inlineStyleParser; | ||
var parse = /*@__PURE__*/getDefaultExportFromCjs(inlineStyleParser); | ||
@@ -280,47 +278,39 @@ /** | ||
* | ||
* @example | ||
* // returns { 'line-height': '42' } | ||
* StyleToObject('line-height: 42;'); | ||
* @param style - Inline style. | ||
* @param iterator - Iterator. | ||
* @returns - Style object or null. | ||
* | ||
* @param {String} style - The inline style. | ||
* @param {Function} [iterator] - The iterator function. | ||
* @return {null|Object} | ||
* @example Parsing inline style to object: | ||
* | ||
* ```js | ||
* import parse from 'style-to-object'; | ||
* parse('line-height: 42;'); // { 'line-height': '42' } | ||
* ``` | ||
*/ | ||
function StyleToObject(style, iterator) { | ||
var output = null; | ||
if (!style || typeof style !== 'string') { | ||
return output; | ||
} | ||
var declaration; | ||
var declarations = parse(style); | ||
var hasIterator = typeof iterator === 'function'; | ||
var property; | ||
var value; | ||
for (var i = 0, len = declarations.length; i < len; i++) { | ||
declaration = declarations[i]; | ||
property = declaration.property; | ||
value = declaration.value; | ||
if (hasIterator) { | ||
iterator(property, value, declaration); | ||
} else if (value) { | ||
output || (output = {}); | ||
output[property] = value; | ||
var styleObject = null; | ||
if (!style || typeof style !== 'string') { | ||
return styleObject; | ||
} | ||
} | ||
return output; | ||
var declarations = parse(style); | ||
var hasIterator = typeof iterator === 'function'; | ||
declarations.forEach(function (declaration) { | ||
if (declaration.type !== 'declaration') { | ||
return; | ||
} | ||
var property = declaration.property, value = declaration.value; | ||
if (hasIterator) { | ||
iterator(property, value, declaration); | ||
} | ||
else if (value) { | ||
styleObject = styleObject || {}; | ||
styleObject[property] = value; | ||
} | ||
}); | ||
return styleObject; | ||
} | ||
styleToObject.exports = StyleToObject; | ||
styleToObject.exports.default = StyleToObject; // ESM support | ||
return StyleToObject; | ||
var styleToObjectExports = styleToObject.exports; | ||
var index = /*@__PURE__*/getDefaultExportFromCjs(styleToObjectExports); | ||
return index; | ||
})); | ||
//# sourceMappingURL=style-to-object.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).StyleToObject=t()}(this,(function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var t={exports:{}},r=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,n=/\n/g,o=/^\s*/,i=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,u=/^:\s*/,c=/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/,f=/^[;\s]*/,a=/^\s+|\s+$/g,s="";function l(e){return e?e.replace(a,s):s}var p=function(e,t){if("string"!=typeof e)throw new TypeError("First argument must be a string");if(!e)return[];t=t||{};var a=1,p=1;function h(e){var t=e.match(n);t&&(a+=t.length);var r=e.lastIndexOf("\n");p=~r?e.length-r:p+e.length}function v(){var e={line:a,column:p};return function(t){return t.position=new d(e),g(),t}}function d(e){this.start=e,this.end={line:a,column:p},this.source=t.source}function m(r){var n=new Error(t.source+":"+a+":"+p+": "+r);if(n.reason=r,n.filename=t.source,n.line=a,n.column=p,n.source=e,!t.silent)throw n}function y(t){var r=t.exec(e);if(r){var n=r[0];return h(n),e=e.slice(n.length),r}}function g(){y(o)}function x(e){var t;for(e=e||[];t=w();)!1!==t&&e.push(t);return e}function w(){var t=v();if("/"==e.charAt(0)&&"*"==e.charAt(1)){for(var r=2;s!=e.charAt(r)&&("*"!=e.charAt(r)||"/"!=e.charAt(r+1));)++r;if(r+=2,s===e.charAt(r-1))return m("End of comment missing");var n=e.slice(2,r-2);return p+=2,h(n),e=e.slice(r),p+=2,t({type:"comment",comment:n})}}function b(){var e=v(),t=y(i);if(t){if(w(),!y(u))return m("property missing ':'");var n=y(c),o=e({type:"declaration",property:l(t[0].replace(r,s)),value:n?l(n[0].replace(r,s)):s});return y(f),o}}return d.prototype.content=e,g(),function(){var e,t=[];for(x(t);e=b();)!1!==e&&(t.push(e),x(t));return t}()};function h(e,t){var r,n=null;if(!e||"string"!=typeof e)return n;for(var o,i,u=p(e),c="function"==typeof t,f=0,a=u.length;f<a;f++)o=(r=u[f]).property,i=r.value,c?t(o,i,r):i&&(n||(n={}),n[o]=i);return n}return t.exports=h,t.exports.default=h,e(t.exports)})); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(n="undefined"!=typeof globalThis?globalThis:n||self).StyleToObject=e()}(this,(function(){"use strict";function n(n){return n&&n.__esModule&&Object.prototype.hasOwnProperty.call(n,"default")?n.default:n}var e=/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,r=/\n/g,t=/^\s*/,o=/^(\*?[-#/*\\\w]+(\[[0-9a-z_-]+\])?)\s*/,i=/^:\s*/,u=/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^)]*?\)|[^};])+)/,c=/^[;\s]*/,f=/^\s+|\s+$/g,a="";function s(n){return n?n.replace(f,a):a}var l=n((function(n,f){if("string"!=typeof n)throw new TypeError("First argument must be a string");if(!n)return[];f=f||{};var l=1,p=1;function h(n){var e=n.match(r);e&&(l+=e.length);var t=n.lastIndexOf("\n");p=~t?n.length-t:p+n.length}function v(){var n={line:l,column:p};return function(e){return e.position=new d(n),g(),e}}function d(n){this.start=n,this.end={line:l,column:p},this.source=f.source}function m(e){var r=new Error(f.source+":"+l+":"+p+": "+e);if(r.reason=e,r.filename=f.source,r.line=l,r.column=p,r.source=n,!f.silent)throw r}function y(e){var r=e.exec(n);if(r){var t=r[0];return h(t),n=n.slice(t.length),r}}function g(){y(t)}function w(n){var e;for(n=n||[];e=b();)!1!==e&&n.push(e);return n}function b(){var e=v();if("/"==n.charAt(0)&&"*"==n.charAt(1)){for(var r=2;a!=n.charAt(r)&&("*"!=n.charAt(r)||"/"!=n.charAt(r+1));)++r;if(r+=2,a===n.charAt(r-1))return m("End of comment missing");var t=n.slice(2,r-2);return p+=2,h(t),n=n.slice(r),p+=2,e({type:"comment",comment:t})}}function A(){var n=v(),r=y(o);if(r){if(b(),!y(i))return m("property missing ':'");var t=y(u),f=n({type:"declaration",property:s(r[0].replace(e,a)),value:t?s(t[0].replace(e,a)):a});return y(c),f}}return d.prototype.content=n,g(),function(){var n,e=[];for(w(e);n=A();)!1!==n&&(e.push(n),w(e));return e}()}));return function(n,e){var r=null;if(!n||"string"!=typeof n)return r;var t=l(n),o="function"==typeof e;return t.forEach((function(n){if("declaration"===n.type){var t=n.property,i=n.value;o?e(t,i,n):i&&((r=r||{})[t]=i)}})),r}})); | ||
//# sourceMappingURL=style-to-object.min.js.map |
{ | ||
"name": "style-to-object", | ||
"version": "0.4.4", | ||
"version": "1.0.0", | ||
"description": "Converts inline style to object.", | ||
"author": "Mark <mark@remarkablemark.org>", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"module": "index.mjs", | ||
"main": "cjs/index.js", | ||
"module": "esm/index.mjs", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"import": "./index.mjs", | ||
"require": "./index.js" | ||
"import": "./esm/index.mjs", | ||
"require": "./cjs/index.js" | ||
}, | ||
"scripts": { | ||
"build": "rollup --config --failAfterWarnings", | ||
"clean": "rm -rf dist", | ||
"build": "run-s build:*", | ||
"build:cjs": "tsc --outDir cjs", | ||
"build:umd": "rollup --config --failAfterWarnings", | ||
"clean": "rm -rf cjs coverage dist", | ||
"lint": "eslint --ignore-path .gitignore .", | ||
"lint:fix": "npm run lint -- --fix", | ||
"lint:dts": "dtslint .", | ||
"lint:tsc": "tsc --noEmit", | ||
"_postinstall": "husky install", | ||
"postpublish": "pinst --enable", | ||
"prepublishOnly": "pinst --disable && run-s lint lint:dts test clean build", | ||
"prepublishOnly": "pinst --disable && run-s lint lint:tsc test clean build", | ||
"test": "jest", | ||
"test:ci": "CI=true jest --ci --colors --coverage", | ||
"test:esm": "node --test __tests__/index.test.mjs", | ||
"test:esm": "npm run build:cjs && node --test __tests__", | ||
"test:watch": "npm run test -- --watch" | ||
@@ -45,3 +45,3 @@ }, | ||
"dependencies": { | ||
"inline-style-parser": "0.1.1" | ||
"inline-style-parser": "0.2.2" | ||
}, | ||
@@ -51,7 +51,9 @@ "devDependencies": { | ||
"@commitlint/config-conventional": "17.8.0", | ||
"@definitelytyped/dtslint": "0.0.182", | ||
"@rollup/plugin-commonjs": "25.0.5", | ||
"@rollup/plugin-commonjs": "25.0.7", | ||
"@rollup/plugin-node-resolve": "15.2.3", | ||
"@rollup/plugin-terser": "0.4.4", | ||
"@typescript-eslint/parser": "5.62.0", | ||
"@rollup/plugin-typescript": "11.1.5", | ||
"@types/jest": "29.5.5", | ||
"@typescript-eslint/eslint-plugin": "6.7.5", | ||
"@typescript-eslint/parser": "6.7.5", | ||
"eslint": "8.51.0", | ||
@@ -65,12 +67,12 @@ "eslint-plugin-prettier": "5.0.1", | ||
"prettier": "3.0.3", | ||
"rollup": "4.1.0", | ||
"rollup": "4.1.1", | ||
"ts-jest": "29.1.1", | ||
"typescript": "5.2.2" | ||
}, | ||
"files": [ | ||
"/cjs", | ||
"/dist", | ||
"index.d.ts", | ||
"index.js", | ||
"index.mjs" | ||
"/esm" | ||
], | ||
"license": "MIT" | ||
} |
@@ -14,3 +14,4 @@ # style-to-object | ||
```js | ||
var parse = require('style-to-object'); | ||
import parse from 'style-to-object'; | ||
parse('color: #C0FFEE; background: #BADA55;'); | ||
@@ -52,12 +53,14 @@ ``` | ||
Import the module: | ||
Import with ES Modules: | ||
```js | ||
// CommonJS | ||
const parse = require('style-to-object'); | ||
// ES Modules | ||
import parse from 'style-to-object'; | ||
``` | ||
Require with CommonJS: | ||
```js | ||
const parse = require('style-to-object').default; | ||
``` | ||
Parse single declaration: | ||
@@ -132,3 +135,3 @@ | ||
```js | ||
parse('color: #f00', function() {}); // null | ||
parse('color: #f00', () => {}); // null | ||
``` | ||
@@ -141,3 +144,3 @@ | ||
```js | ||
parse('color: #f00', function(name, value, declaration) { | ||
parse('color: #f00', (name, value, declaration) => { | ||
console.log(name); // 'color' | ||
@@ -168,40 +171,12 @@ console.log(value); // '#f00' | ||
## Testing | ||
## Migration | ||
Run tests: | ||
### v1 | ||
```sh | ||
npm test | ||
``` | ||
Migrated to TypeScript. Iterator excludes `Comment`. CommonJS requires the `.default` key: | ||
Run tests in watch mode: | ||
```sh | ||
npm run test:watch | ||
```js | ||
const parse = require('style-to-object').default; | ||
``` | ||
Run tests with coverage: | ||
```sh | ||
npm run test:coverage | ||
``` | ||
Lint files: | ||
```sh | ||
npm run lint | ||
``` | ||
Fix lint errors: | ||
```sh | ||
npm run lint:fix | ||
``` | ||
Test TypeScript declaration file for style and correctness: | ||
```sh | ||
npm run lint:dts | ||
``` | ||
## Release | ||
@@ -208,0 +183,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12
1
40099
20
344
189
+ Addedinline-style-parser@0.2.2(transitive)
- Removedinline-style-parser@0.1.1(transitive)
Updatedinline-style-parser@0.2.2