Comparing version 1.0.0 to 2.0.0
108
index.js
@@ -6,5 +6,93 @@ 'use strict'; | ||
class Subsume { | ||
static parse(str, id) { | ||
return (new Subsume(id)).parse(str); | ||
static parse(text, id) { | ||
return (new Subsume(id)).parse(text); | ||
} | ||
static parseAll(text, ids) { | ||
if (ids && !Array.isArray(ids)) { | ||
throw new TypeError('IDs is supposed to be an array'); | ||
} | ||
const result = {data: new Map(), rest: text}; | ||
const idList = ids ? ids : Subsume._extractIDs(text); | ||
if (!ids) { | ||
try { | ||
Subsume._checkIntegrity(text); | ||
} catch (error) { | ||
throw new Error(`Could not parse because the string's integrity is compromised: ${error.message}`); | ||
} | ||
} | ||
for (const id of idList) { | ||
if (result.data.get(id)) { | ||
throw new Error('IDs aren\'t supposed to be repeated at the same level in a string'); | ||
} | ||
const res = Subsume.parse(result.rest, id); | ||
result.data.set(id, res.data); | ||
result.rest = res.rest; | ||
} | ||
return result; | ||
} | ||
static _extractIDs(text) { | ||
try { | ||
Subsume._checkIntegrity(text); | ||
} catch (error) { | ||
throw new Error(`Could not extract IDs because the string's integrity is compromised: ${error.message}`); | ||
} | ||
const idRegex = /@@\[(.{32})\]@@.*##\[\1\]##/g; | ||
const idList = []; | ||
let match; | ||
do { | ||
match = idRegex.exec(text); | ||
if (match) { | ||
const [, id] = match; | ||
idList.push(id); | ||
} | ||
} while (match); | ||
return idList; | ||
} | ||
static _checkIntegrity(text) { | ||
const delimiterRegex = /([#|@])\1\[(.{32})\]\1{2}/g; | ||
const ids = new Map(); | ||
const idStack = []; | ||
let match; | ||
do { | ||
match = delimiterRegex.exec(text); | ||
if (match) { | ||
const [, embedToken, id] = match; | ||
if (embedToken === '@') { | ||
let map = ids; | ||
for (const el of idStack) { | ||
map = map.get(el); | ||
} | ||
if (map.get(id)) { | ||
throw new Error('There are duplicate IDs in the same scope.'); | ||
} | ||
map.set(id, new Map()); | ||
idStack.push(id); | ||
} else { | ||
idStack.pop(); | ||
} | ||
} | ||
} while (match); | ||
if (idStack.length !== 0) { | ||
throw new Error('There is a mismatch between prefixes and suffixes'); | ||
} | ||
return ids; | ||
} | ||
constructor(id) { | ||
@@ -20,9 +108,17 @@ if (id && (id.includes('@@[') || id.includes('##['))) { | ||
} | ||
compose(str) { | ||
return this.prefix + str + this.postfix; | ||
compose(text) { | ||
return this.prefix + text + this.postfix; | ||
} | ||
parse(str) { | ||
parse(text) { | ||
try { | ||
Subsume._checkIntegrity(text); | ||
} catch (error) { | ||
throw new Error(`Could not extract IDs because the string's integrity is compromised: ${error.message}`); | ||
} | ||
const ret = {}; | ||
ret.rest = str.replace(this.regex, (m, p1) => { | ||
ret.rest = text.replace(this.regex, (m, p1) => { | ||
if (p1) { | ||
@@ -29,0 +125,0 @@ ret.data = p1; |
{ | ||
"name": "subsume", | ||
"version": "1.0.0", | ||
"description": "Embed data in other data and easily extract it when needed", | ||
"license": "MIT", | ||
"repository": "sindresorhus/subsume", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"subsume", | ||
"embed", | ||
"embedded", | ||
"include", | ||
"insert", | ||
"data", | ||
"string", | ||
"text", | ||
"content", | ||
"compose", | ||
"parse", | ||
"extract", | ||
"encode", | ||
"decode" | ||
], | ||
"dependencies": { | ||
"escape-string-regexp": "^1.0.5", | ||
"unique-string": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "subsume", | ||
"version": "2.0.0", | ||
"description": "Embed data in other data and easily extract it when needed", | ||
"license": "MIT", | ||
"repository": "sindresorhus/subsume", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"subsume", | ||
"embed", | ||
"embedded", | ||
"include", | ||
"insert", | ||
"data", | ||
"string", | ||
"text", | ||
"content", | ||
"compose", | ||
"parse", | ||
"extract", | ||
"encode", | ||
"decode" | ||
], | ||
"dependencies": { | ||
"escape-string-regexp": "^1.0.5", | ||
"unique-string": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # subsume [![Build Status](https://travis-ci.org/sindresorhus/subsume.svg?branch=master)](https://travis-ci.org/sindresorhus/subsume) | ||
``` | ||
$ npm install --save subsume | ||
$ npm install subsume | ||
``` | ||
@@ -29,11 +29,11 @@ | ||
// the text can now be embedded in some other text | ||
// The text can now be embedded in some other text | ||
const output = `some${text} random text`; | ||
//=> 'some@@[7febcd0b3806fbc48c01d7cea4ed1219]@@🦄##[7febcd0b3806fbc48c01d7cea4ed1219]## random text' | ||
// at a later point we extract it | ||
// At a later point we extract it | ||
subsume.parse(output); | ||
//=> {data: '🦄', rest: 'some random text'} | ||
// or in a different process by using the `id` | ||
// Or in a different process by using the `id` | ||
const input = 'some@@[7febcd0b3806fbc48c01d7cea4ed1219]@@🦄##[7febcd0b3806fbc48c01d7cea4ed1219]## random text'; | ||
@@ -106,5 +106,30 @@ Subsume.parse(text, '7febcd0b3806fbc48c01d7cea4ed1219'); | ||
### Subsume.parseAll(text[, idArray]) | ||
Extract embedded data corresponding to all IDs in `idArray`, if specified. Otherwise it will extract embedded data for all top-level IDs. | ||
Returns an object with properties `.data`, a Map with an entry for each parsed ID, and `.rest` for what remains after all the required IDs have been parsed, as seen below: | ||
The input: | ||
``` | ||
some@@[7febcd0b3806fbc48c01d7cea4ed1219]@@🦄##[7febcd0b3806fbc48c01d7cea4ed1219]## random@@[7febcd0b3806fbc48c01d7cea4ed1218]@@🦄##[7febcd0b3806fbc48c01d7cea4ed1218]## text@@[7febcd0b3806fbc48c01d7cea4ed1217]@@🦄##[7febcd0b3806fbc48c01d7cea4ed1217]## | ||
``` | ||
Gives the following output: | ||
``` | ||
{ | ||
data: Map { | ||
'7febcd0b3806fbc48c01d7cea4ed1219' => '🦄', | ||
'7febcd0b3806fbc48c01d7cea4ed1218' => '🦄', | ||
'7febcd0b3806fbc48c01d7cea4ed1217' => '🦄' | ||
}, | ||
rest: 'some random text' | ||
} | ||
``` | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
7952
106
133