Skip to content

Commit 256fd1d

Browse files
author
Leif Shackelford
committed
add: ~ key operator
1 parent 8246389 commit 256fd1d

File tree

8 files changed

+62
-21
lines changed

8 files changed

+62
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "js-moss",
33
"description": "moss is a set of parsing rules for yaml and json",
4-
"version": "0.18.11",
4+
"version": "0.18.13",
55
"homepage": "https://github.com/1e1f/js-moss",
66
"author": {
77
"name": "Leif Shackelford",

src/async.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,24 @@ export const parseFunction = async (
106106
errorPaths: state.errorPaths,
107107
};
108108
}
109-
110109
}
111110
}
112111

113112
export const parseObject = async (current: Moss.ReturnValue) => {
114113
const { state } = current;
115114
const source: any = clone(current.data);
116115
const target = state.target || current.data;
116+
117+
// Expand has precedence
118+
for (const _key of Object.keys(source)) {
119+
if (_key[0] === '~' && _key.indexOf(".") != -1) {
120+
const kp = _key.slice(1);
121+
setValueForKeyPath(source[_key], kp, source);
122+
delete source[_key];
123+
delete target[_key];
124+
}
125+
}
126+
117127
for (const _key of Object.keys(source)) {
118128
let res;
119129
if (!_key) {
@@ -133,12 +143,7 @@ export const parseObject = async (current: Moss.ReturnValue) => {
133143
if (_key[0] === "$") {
134144
key = <any>(await interpolate(current, _key)).data;
135145
} else if (_key[0] == "\\") {
136-
key = key.slice(1);
137-
} else if (_key.indexOf(".") != -1) {
138-
const [first, ...kp] = _key.split(".");
139-
key = first;
140-
val = {};
141-
setValueForKeyPath(source[_key], kp.join("."), val);
146+
key = _key.slice(1);
142147
} else {
143148
key = _key;
144149
}
@@ -155,6 +160,17 @@ export const parseObject = async (current: Moss.ReturnValue) => {
155160
return current;
156161
};
157162

163+
export const wrapFunction = (fn: Function, transformArgs?: (args: any) => any[]) => async (current: Moss.ReturnValue, args: Moss.BranchData, setRes: any) => {
164+
const { data } = await continueWithNewFrame(current, args);
165+
let res;
166+
if (transformArgs) {
167+
res = await fn(...transformArgs(data));
168+
} else {
169+
res = await fn(data);
170+
}
171+
setRes ? await setRes(res) : current.data = res;
172+
}
173+
158174
export const parseArray = async (
159175
layer: Moss.ReturnValue,
160176
input: Moss.BranchData
@@ -550,7 +566,7 @@ addFunctions({
550566
const layer = await continueWithNewFrame(parent, args);
551567
const { data } = layer;
552568
parent.data = sum(data, (v: any) => v);
553-
},
569+
}
554570
});
555571

556572
async function interpolate(layer: Moss.ReturnValue, input: any) {

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ import * as Async from "./async";
77
import * as Sync from "./sync";
88
export { Async, Sync };
99

10+
import './moreFunctions';
11+
1012
export { Moss } from './types';

src/interpolate/async.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function tokenize(str: string, options: Expand.Options) {
6565
str = str.slice(0, str.length - 1);
6666
}
6767
const res = str && await fn(str, { defer, required, sourceMap });
68-
if (required && !(res || check(res, Number))) {
68+
if (required && res === undefined) {
6969
throw {
7070
message: `${str} doesn't exist, and is required.\nignore (non-strict) with: ${str}?`,
7171
source: str
@@ -86,7 +86,7 @@ export async function tokenize(str: string, options: Expand.Options) {
8686
str = str.slice(0, str.length - 1);
8787
}
8888
const res = str && fn(str, { defer, required, sourceMap });
89-
if (required && !(res || check(res, Number))) {
89+
if (required && res === undefined) {
9090
throw {
9191
message: `${str} doesn't exist, and is required.\nignore (non-strict) with: ${str}?`,
9292
source: str

src/interpolate/sync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function tokenize(str: string, options: Expand.Options) {
6565
str = str.slice(0, str.length - 1);
6666
}
6767
const res = str && fn(str, { defer, required, sourceMap });
68-
if (required && !(res || check(res, Number))) {
68+
if (required && res === undefined) {
6969
throw {
7070
message: `${str} doesn't exist, and is required.\nignore (non-strict) with: ${str}?`,
7171
source: str

src/sync.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ export const parseObject = (current: Moss.ReturnValue) => {
114114
const { state } = current;
115115
const source: any = clone(current.data);
116116
const target = state.target || current.data;
117+
118+
// Expand has precedence
119+
for (const _key of Object.keys(source)) {
120+
if (_key[0] === '~' && _key.indexOf(".") != -1) {
121+
const kp = _key.slice(1);
122+
setValueForKeyPath(source[_key], kp, source);
123+
delete source[_key];
124+
delete target[_key];
125+
}
126+
}
127+
117128
for (const _key of Object.keys(source)) {
118129
let res;
119130
if (!_key) {
@@ -133,9 +144,10 @@ export const parseObject = (current: Moss.ReturnValue) => {
133144
if (_key[0] === "$") {
134145
key = <any>(interpolate(current, _key)).data;
135146
} else if (_key[0] == "\\") {
136-
key = key.slice(1);
137-
} else if (_key.indexOf(".") != -1) {
138-
const [first, ...kp] = _key.split(".");
147+
key = _key.slice(1);
148+
} else if (_key[0] === '~' && _key.indexOf(".") != -1) {
149+
key = _key.slice(1);
150+
const [first, ...kp] = key.split(".");
139151
key = first;
140152
val = {};
141153
setValueForKeyPath(source[_key], kp.join("."), val);
@@ -155,6 +167,19 @@ export const parseObject = (current: Moss.ReturnValue) => {
155167
return current;
156168
};
157169

170+
171+
export const wrapFunction = (fn: Function, transformArgs?: (args: any) => any[]) => async (current: Moss.ReturnValue, args: Moss.BranchData, setRes: any) => {
172+
const { data } = continueWithNewFrame(current, args);
173+
let res;
174+
if (transformArgs) {
175+
res = fn(...transformArgs(data));
176+
} else {
177+
res = fn(data);
178+
}
179+
setRes ? setRes(res) : current.data = res;
180+
}
181+
182+
158183
export const parseArray = (
159184
layer: Moss.ReturnValue,
160185
input: Moss.BranchData

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export declare namespace Moss {
7474

7575
type ErrorReporter = (error: Error) => Error;
7676

77-
type Function<T> = (current: Moss.ReturnValue, args: any) => T;
77+
type Function<T> = (current: Moss.ReturnValue, args: any, setter?: (...args: any[]) => void) => T;
7878
type Resolver<T> = {
7979
name?: string;
8080
match: (uri: string) => boolean;

src/util.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export const handleError = (e: Moss.Error, layer: Moss.ReturnValue, input?: Moss
4141
if (getErrorReporter()) {
4242
throw (getErrorReporter()(error));
4343
} else {
44-
throw {
45-
name: 'MossError',
46-
...error
47-
}
44+
console.error(error)
45+
throw new Error(error.message);
4846
}
49-
}
47+
}

0 commit comments

Comments
 (0)