-
Notifications
You must be signed in to change notification settings - Fork 14
/
return.js
110 lines (97 loc) · 3.82 KB
/
return.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var copy = require("../../../deep-copy");
var apply = copy.apply;
var util = require("util");
var jp = require("jsonpath");
var ReturnImplError = function(message) {
this.message = message;
this.name = "Command 'return' implementation error";
}
ReturnImplError.prototype = Object.create(Error.prototype);
ReturnImplError.prototype.constructor = ReturnImplError;
module.exports = {
name: "return",
synonims: {
"return": "return"
},
"internal aliases":{
"path": "path",
"var":"path",
"as": "as",
"type":"as"
},
defaultProperty: {
"return": "path",
},
help: {
synopsis: "Get deep copy of variable and set context",
name: {
"default": "return",
synonims: []
},
input:["any"],
output:"type of variable",
"default param": "path",
params: [
{
name: "path",
synopsis: "Json path to selected value (optional). If 'value' is not assigned then storage will be restored.",
type:["json-path"],
synonims: ["var"],
"default value": "$"
},{
name: "as",
synopsis: "Json as to selected value (optional). If 'value' is not assigned then storage will be restored.",
type:["json-path"],
synonims: ["type"],
"default value": "$"
}
],
example: {
description: "Inspect variables",
code: "<?json \r\n \"Hello\" \r\n?>\r\nset(\"str\")\r\n\r\n<?javascript \r\n var notNull = function(item){\r\n return item != undefined\r\n \r\n }; \r\n?>\r\nset(\"functions\")\r\n\r\nload(\r\n ds:\"47611d63-b230-11e6-8a1a-0f91ca29d77e_2016_02\", \r\n as:'json'\r\n)\r\n\r\nselect(\"$.metadata.dataset.commit\")\r\n\r\nset(var:\"commitNote\", val:\"$[0].note\")\r\nget(\"str\")\r\ninfo()\r\nget(\"functions.notNull\")\r\ninfo()\r\nget(\"commitNote\")\r\ninfo()\r\n// equals for previus\r\nget(\"$.commitNote\")\r\ninfo()\r\nlog()\r\n"
}
},
execute: function(command, state) {
try {
var getProperty = function(d, path) {
var result = undefined;
jp.apply(d, path, function(value) {
if (util.isUndefined(result)) {
result = value;
} else {
if (!util.isArray(result)) {
result = [result]
}
result.push(value)
}
return value
})
return result
}
if (util.isUndefined(command.settings) || util.isUndefined(command.settings.path) || command.settings.path == "" || command.settings.path == "$") {
// state.head = {
// data: state.head.data,
// type: command.settings.as || "json"
// }
// if (util.isFunction(state.head.data)) state.head.type = 'function'
return state;
}
// if (command.settings.path.indexOf("_")==0){
// var res = state.storage._[command.settings.path.substring(2,command.settings.path.length)];
// state.head={
// data: res,
// type: typeof res
// }
// }else{
state.head = {
data: copy(getProperty(state.storage, command.settings.path)),
type: command.settings.as || "json"
}
if (util.isFunction(state.head.data)) state.head.type = 'function'
// }
return state
} catch (e) {
throw new ReturnImplError(e.toString())
}
}
}