Socket
Socket
Sign inDemoInstall

crack-json

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crack-json - npm Package Compare versions

Comparing version 1.2.3 to 1.3.0

25

dist/utilities/extractJson.js

@@ -20,2 +20,3 @@ "use strict";

const parser = configuration && configuration.parser ? configuration.parser : defaultParser;
const filter = configuration && configuration.filter;
const foundObjects = [];

@@ -27,21 +28,23 @@ const rule = /["[{]/g;

const offsetSubject = subject.slice(subjectOffset);
const match = rule.exec(offsetSubject);
const openCharacterMatch = rule.exec(offsetSubject);
if (!match) {
if (!openCharacterMatch) {
break;
}
const openCharacter = match[0];
const openCharacter = openCharacterMatch[0];
const closeCharacter = closeCharacterMap[openCharacter];
const startIndex = match.index;
const startIndex = openCharacterMatch.index;
let haystack = offsetSubject.slice(startIndex);
while (haystack.length) {
try {
const result = parser(haystack);
foundObjects.push(result);
subjectOffset += startIndex + haystack.length;
rule.lastIndex = 0;
break;
} catch (error) {//
if (!filter || filter(haystack)) {
try {
const result = parser(haystack);
foundObjects.push(result);
subjectOffset += startIndex + haystack.length;
rule.lastIndex = 0;
break;
} catch (error) {//
}
}

@@ -48,0 +51,0 @@

@@ -76,3 +76,3 @@ {

},
"version": "1.2.3"
"version": "1.3.0"
}

@@ -16,2 +16,3 @@ # crack-json 🥊

* [Usage](#usage)
* [Filtering out matches](#filtering-out-matches)

@@ -67,6 +68,8 @@ ## Use case

/**
* @property filter Used to filter out strings before attempting to decode them.
* @property parser A parser used to extract JSON from the suspected strings. Default: `JSON.parse`.
*/
type ExtractJsonConfigurationType = {|
+parser?: (input: string) => any
+filter?: (input: string) => boolean,
+parser?: (input: string) => any,
|};

@@ -124,1 +127,43 @@

```
### Filtering out matches
You can use `filter` to exclude strings before they are parsed using an arbitrary condition. This will improve performance and reduce output only to the desirable objects, e.g.
```html
import {
extractJson
} from 'crack-json';
const payload = `
<script>
const foo = {
cinemaId: '1',
};
const bar = {
venueId: '1',
};
const baz = {
userId: '1',
};
</script>
`;
console.log(extractJson(payload, {
filter: (input) => {
return input.includes('userId')
},
}));
```
Output:
```js
[
{
userId: '1',
},
]
```
// @flow
type ExtractJsonConfigurationType = {|
+filter?: (input: string) => boolean,
// eslint-disable-next-line flowtype/no-weak-types

@@ -22,2 +24,3 @@ +parser?: (input: string) => any,

const parser = configuration && configuration.parser ? configuration.parser : defaultParser;
const filter = configuration && configuration.filter;

@@ -33,12 +36,12 @@ const foundObjects = [];

const match = rule.exec(offsetSubject);
const openCharacterMatch = rule.exec(offsetSubject);
if (!match) {
if (!openCharacterMatch) {
break;
}
const openCharacter = match[0];
const openCharacter = openCharacterMatch[0];
const closeCharacter = closeCharacterMap[openCharacter];
const startIndex = match.index;
const startIndex = openCharacterMatch.index;

@@ -48,14 +51,16 @@ let haystack = offsetSubject.slice(startIndex);

while (haystack.length) {
try {
const result = parser(haystack);
if (!filter || filter(haystack)) {
try {
const result = parser(haystack);
foundObjects.push(result);
foundObjects.push(result);
subjectOffset += startIndex + haystack.length;
subjectOffset += startIndex + haystack.length;
rule.lastIndex = 0;
rule.lastIndex = 0;
break;
} catch (error) {
//
break;
} catch (error) {
//
}
}

@@ -62,0 +67,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc