@putout/operator-json 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +50 -0
  2. package/lib/json.js +20 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -16,14 +16,46 @@ npm i putout @putout/operator-json
16
16
  ### `__json`
17
17
 
18
18
  ```js
19
+ import {operator} from 'putout';
20
+
21
+ const {__json} = operator;
22
+
19
23
  export const traverse = ({push}) => ({
20
24
  [__json]: push,
21
25
  });
22
26
  ```
23
27
 
28
+ ### `__yaml`
29
+
30
+ ```js
31
+ import {operator} from 'putout';
32
+
33
+ const {__yaml} = operator;
34
+
35
+ export const traverse = ({push}) => ({
36
+ [__yaml]: push,
37
+ });
38
+ ```
39
+
40
+ ### `__ignore`
41
+
42
+ ```js
43
+ import {operator} from 'putout';
44
+
45
+ const {__ignore} = operator;
46
+
47
+ export const traverse = ({push}) => ({
48
+ [__ignore]: push,
49
+ });
50
+ ```
51
+
24
52
  ### `__filesystem`
25
53
 
26
54
  ```js
55
+ import {operator} from 'putout';
56
+
57
+ const {__filesystem} = operator;
58
+
27
59
  export const traverse = ({push}) => ({
28
60
  [__filesystem]: (path) => {
29
61
  push(path);
@@ -31,9 +63,24 @@ export const traverse = ({push}) => ({
31
63
  });
32
64
  ```
33
65
 
66
+ ### `isJSON(source: string)`
67
+
68
+ ```js
69
+ isJSON(`__putout_processor_json({"hello": "world"});`);
70
+ // returns
71
+ true;
72
+
73
+ isJSON(`hello({"hello": "world"});`);
74
+ // returns
75
+ false;
76
+ ```
77
+
34
78
  ### `toJS(source: string, name?: string)`;
35
79
 
36
80
  ```js
81
+ import {operator} from 'putout';
82
+
83
+ const {__filesystem, toJS} = operator;
37
84
  toJS('{"hello": "world"}');
38
85
  // returns
39
86
  `__putout_processor_json('{"hello": "world"});`;
@@ -46,6 +93,9 @@ toJS('{"hello": "world"}', __filesystem);
46
93
  ### `fromJS(source: string, name?: string)`;
47
94
 
48
95
  ```js
96
+ import {operator} from 'putout';
97
+
98
+ const {fromJS} = operator;
49
99
  fromJS(`__putout_processor_json('{"hello": "world"}'`);
50
100
  // returns
51
101
  `{"hello": "world"}`;
package/lib/json.js CHANGED
@@ -2,15 +2,22 @@
2
2
 
3
3
  const removeBlankLines = require('remove-blank-lines');
4
4
 
5
- const maybeNewline = (a) => a.at(-1) === '\n' ? a : `${a}\n`;
6
- const createPrefix = (name) => `${name}(`;
5
+ const cut = (a) => a.slice(0, a.indexOf('('));
6
+ const createPrefix = (name) => `${cut(name)}(`;
7
7
  const createSuffix = () => ');\n';
8
+ const maybeNewline = (a) => a.at(-1) === '\n' ? a : `${a}\n`;
9
+
10
+ const __json = '__putout_processor_json(__object)';
11
+ const __yaml = '__putout_processor_yaml(__object)';
12
+ const __filesystem = '__putout_processor_filesystem(__object)';
13
+ const __ignore = '__putout_processor_ignore(__array)';
8
14
 
9
- const __json = '__putout_processor_json';
10
- const __filesystem = '__putout_processor_filesystem';
15
+ const TYPES = [__json, __yaml, __filesystem, __ignore].map(cut);
11
16
 
12
17
  module.exports.__json = __json;
18
+ module.exports.__yaml = __yaml;
13
19
  module.exports.__filesystem = __filesystem;
20
+ module.exports.__ignore = __ignore;
14
21
 
15
22
  module.exports.toJS = (source, name = __json) => {
16
23
  const prefix = createPrefix(name);
@@ -27,3 +34,12 @@ module.exports.fromJS = (source, name = __json) => {
27
34
 
28
35
  return maybeNewline(removeBlankLines(sliced));
29
36
  };
37
+
38
+ module.exports.isJSON = (source) => {
39
+ for (const type of TYPES) {
40
+ if (!source.indexOf(type))
41
+ return true;
42
+ }
43
+
44
+ return false;
45
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-json",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout operator adds ability to json referenced variables that was not defined",