attributes-parser
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -1,97 +0,86 @@ | ||
import moo from "moo"; | ||
import jsonLoose from "json-loose"; | ||
const WhiteSpace = /[ \t\v\f\ufeff]+/; | ||
const BooleanLiteral = new RegExp("(?<==)(?:true|false)"); | ||
const NumericLiteral = new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"); | ||
const SingleQuotedLiteral = new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`); | ||
const DoubleQuotedLiteral = new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'); | ||
const UnquotedLiteral = new RegExp("(?<==)[^\"\\s'`=<>\\x00]+"); | ||
const AttributeName = /(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/; | ||
function formatString(text) { | ||
const value = typeof text === "string" && /^(['"]).*?\1$/.test(text) ? ( | ||
import o from "moo"; | ||
import u from "json-loose"; | ||
const c = /[ \t\v\f\ufeff]+/, f = new RegExp("(?<==)(?:true|false)"), m = new RegExp("(?<==)-?(?:(?:0[xX][\\da-fA-F](?:_?[\\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|-?0n|-?[1-9](?:_?\\d)*n|(?:(?:0(?!\\d)|0\\d*[89]\\d*|[1-9](?:_?\\d)*)(?:\\.(?:\\d(?:_?\\d)*)?)?|\\.\\d(?:_?\\d)*)(?:[eE][+-]?\\d(?:_?\\d)*)?|-?0[0-7]+)"), b = new RegExp(`(?<==)'(?!.*&[0-9a-zA-Z]+;)[^'\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*'`), d = new RegExp('(?<==)"(?!.*&[0-9a-zA-Z]+;)[^"\\\\]*(?:\\\\.|\\\\n[^"\\\\]*|&[^0-9a-zA-Z;]*)*"'), x = new RegExp("(?<==)[^\"\\s'`=<>\\x00]+"), L = /(?:(?![\s\x00\x22\x27\x3E\x2F\x3D\x00-\x1F\x7F-\x9F])[^\s\x00-\x1F\x7F-\x9F\x22\x27\x3E\x2F\x3D])+/; | ||
function n(t) { | ||
const e = typeof t == "string" && /^(['"]).*?\1$/.test(t) ? ( | ||
// omit quotes | ||
text.slice(1, -1) | ||
) : text; | ||
if (value.startsWith("[") && value.endsWith("]") || value.startsWith("{") && value.endsWith("}")) { | ||
return JSON.parse(jsonLoose(value)); | ||
} | ||
return value; | ||
t.slice(1, -1) | ||
) : t; | ||
return e.startsWith("[") && e.endsWith("]") || e.startsWith("{") && e.endsWith("}") ? JSON.parse(u(e)) : e; | ||
} | ||
function serialize(attrs) { | ||
let acc = ""; | ||
for (const key in attrs) { | ||
const value = attrs[key]; | ||
switch (typeof value) { | ||
function g(t) { | ||
let e = ""; | ||
for (const r in t) { | ||
const a = t[r]; | ||
switch (typeof a) { | ||
case "object": | ||
acc += ` ${key}='${JSON.stringify(value)}'`; | ||
e += ` ${r}='${JSON.stringify(a)}'`; | ||
break; | ||
case "string": | ||
acc += ` ${key}="${value}"`; | ||
e += ` ${r}="${a}"`; | ||
break; | ||
case "number": | ||
case "boolean": | ||
acc += ` ${key}=${value}`; | ||
e += ` ${r}=${a}`; | ||
break; | ||
} | ||
} | ||
return acc.slice(1); | ||
return e.slice(1); | ||
} | ||
const lexer = moo.states({ | ||
const l = o.states({ | ||
main: { | ||
WhiteSpace: { match: WhiteSpace, lineBreaks: true }, | ||
WhiteSpace: { match: c, lineBreaks: !0 }, | ||
BooleanLiteral: { | ||
match: BooleanLiteral, | ||
value(x) { | ||
return x === "true" ? true : false; | ||
match: f, | ||
value(t) { | ||
return t === "true"; | ||
} | ||
}, | ||
NumericLiteral: { | ||
match: NumericLiteral, | ||
value(x) { | ||
const n = Number(x); | ||
return Number.isNaN(n) ? Number(x.replace(/_|n$/g, "")) : Number(x); | ||
match: m, | ||
value(t) { | ||
const e = Number(t); | ||
return Number.isNaN(e) ? Number(t.replace(/_|n$/g, "")) : Number(t); | ||
} | ||
}, | ||
SingleQuotedValue: { | ||
match: SingleQuotedLiteral, | ||
value: formatString, | ||
match: b, | ||
value: n, | ||
type: () => "StringLiteral" | ||
}, | ||
DoubleQuotedLiteral: { | ||
match: DoubleQuotedLiteral, | ||
value: formatString, | ||
match: d, | ||
value: n, | ||
type: () => "StringLiteral" | ||
}, | ||
UnquotedLiteral: { | ||
match: UnquotedLiteral, | ||
value: formatString, | ||
match: x, | ||
value: n, | ||
type: () => "StringLiteral" | ||
}, | ||
AttributeName, | ||
AttributeName: L, | ||
Separator: "=" | ||
} | ||
}); | ||
function parseAttrs(input) { | ||
let currentKey = null; | ||
const tokens = lexer.reset(input); | ||
const attrs = {}; | ||
Object.defineProperties(attrs, { | ||
function N(t) { | ||
let e = null; | ||
const r = l.reset(t), a = {}; | ||
Object.defineProperties(a, { | ||
toString: { | ||
writable: false, | ||
enumerable: false, | ||
configurable: false, | ||
value: () => serialize(attrs) | ||
writable: !1, | ||
enumerable: !1, | ||
configurable: !1, | ||
value: () => g(a) | ||
}, | ||
getTokens: { | ||
writable: false, | ||
enumerable: false, | ||
configurable: false, | ||
value: () => Array.from(lexer.reset(input)) | ||
writable: !1, | ||
enumerable: !1, | ||
configurable: !1, | ||
value: () => Array.from(l.reset(t)) | ||
} | ||
}); | ||
for (const { type, value } of tokens) { | ||
switch (type) { | ||
for (const { type: i, value: s } of r) | ||
switch (i) { | ||
case "AttributeName": | ||
currentKey = value; | ||
attrs[currentKey] = currentKey; | ||
e = s, a[e] = e; | ||
break; | ||
@@ -101,13 +90,9 @@ case "BooleanLiteral": | ||
case "StringLiteral": | ||
if (currentKey) { | ||
attrs[currentKey] = value; | ||
currentKey = null; | ||
} | ||
e && (a[e] = s, e = null); | ||
break; | ||
} | ||
} | ||
return attrs; | ||
return a; | ||
} | ||
export { | ||
parseAttrs as default | ||
N as default | ||
}; |
{ | ||
"name": "attributes-parser", | ||
"description": "Parsing and tokenizing attributes string", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"publishConfig": { | ||
@@ -29,3 +29,4 @@ "access": "public" | ||
"type": "module", | ||
"main": "dist/index.umd.cjs", | ||
"main": "dist/index.cjs", | ||
"browser": "dist/index.umd.cjs", | ||
"module": "dist/index.js", | ||
@@ -36,3 +37,3 @@ "types": "dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.umd.cjs", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
@@ -47,8 +48,5 @@ } | ||
"scripts": { | ||
"start": "npm run prod", | ||
"prod": "vite build && vite", | ||
"dev": "vite build:ssr --watch", | ||
"build": "npm run build:ssr && npm run build:prod && npm run types", | ||
"build:ssr": "vite build --ssr src/index.ts", | ||
"build:prod": "vite build", | ||
"start": "vite", | ||
"dev": "vite build --watch", | ||
"build": "vite build && npm run types", | ||
"test": "vitest", | ||
@@ -66,3 +64,3 @@ "coverage": "vitest run --coverage", | ||
"@semantic-release/git": "^10.0.1", | ||
"@types/moo": "0.5.7", | ||
"@types/moo": "^0.5.7", | ||
"doogu": "3.2.7", | ||
@@ -69,0 +67,0 @@ "semantic-release": "^22.0.5" |
@@ -7,12 +7,18 @@ # Attributes Parser | ||
You can install this module using npm or yarn, it's just `3.24 kB | min: 1.94 kB`: | ||
You can install this module using npm or yarn, it's only `2.68 kB | min: 1.10 kB`: | ||
```bash | ||
npm i attributes-parser | ||
# or | ||
yarn add attributes-parser | ||
``` | ||
Alternatively, you can also include this module directly in your HTML file from [CDN](https://www.jsdelivr.com/package/npm/attributes-parser?tab=files&path=dist): | ||
```html | ||
<script type="module"> | ||
import jsonLoose from 'https://cdn.jsdelivr.net/npm/attributes-parser/+esm' | ||
</script> | ||
``` | ||
## Usage | ||
@@ -19,0 +25,0 @@ |
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
10
226
0
20739
257
3