@jsonic/directive
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -1,6 +0,6 @@ | ||
import { AltAction, Plugin } from '@jsonic/jsonic-next'; | ||
import { StateAction, Plugin } from '@jsonic/jsonic-next'; | ||
declare type DirectiveOptions = { | ||
name: string; | ||
open: string; | ||
action: AltAction | string; | ||
action: StateAction | string; | ||
close?: string; | ||
@@ -7,0 +7,0 @@ rules?: string | string[]; |
"use strict"; | ||
/* Copyright (c) 2021 Richard Rodger, MIT License */ | ||
/* Copyright (c) 2021-2022 Richard Rodger, MIT License */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -12,7 +12,10 @@ exports.Directive = void 0; | ||
let close = options.close; | ||
let action = options.action; | ||
if ('string' === typeof action) { | ||
let path = action; | ||
let action; | ||
if ('string' === typeof options.action) { | ||
let path = options.action; | ||
action = (rule) => (rule.node = jsonic.util.prop(jsonic.options, path)); | ||
} | ||
else { | ||
action = options.action; | ||
} | ||
let token = {}; | ||
@@ -88,3 +91,3 @@ let openTN = '#D_open_' + name; | ||
.clear() | ||
.bo((rule) => (rule.node = {})) | ||
.bo((rule) => ((rule.node = {}), undefined)) | ||
.open([ | ||
@@ -98,3 +101,9 @@ { | ||
]) | ||
.bc((...all) => action(...all)) | ||
// .bc((...all: any[]) => (action as any)(...all)) | ||
.bc(function (rule, ctx) { | ||
let out = action.call(this, rule, ctx); | ||
if (out === null || out === void 0 ? void 0 : out.isToken) { | ||
return out; | ||
} | ||
}) | ||
.close(null != close ? [{ s: [CLOSE] }, { s: [CA, CLOSE] }] : [])); | ||
@@ -101,0 +110,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).JsonicDirective=e()}}((function(){var e={};Object.defineProperty(e,"__esModule",{value:!0}),e.Directive=void 0;const n=(e,n)=>{let l=("string"==typeof n.rules?n.rules.split(/\s*,\s*/):n.rules||[]).filter(e=>""!==e),t=n.name,o=n.open,i=n.close,r=n.action;if("string"==typeof r){let n=r;r=l=>l.node=e.util.prop(e.options,n)}let s={},u="#D_open_"+t,d="#D_close_"+t,c=e.fixed(o),f=null==i?null:e.fixed(i);if(null!=c)throw new Error("Directive open token already in use: "+o);s[u]=o,null==f&&null!=i&&(s[d]=i),e.options({fixed:{token:s},error:{[t+"_close"]:null==i?null:"directive "+t+' close "'+i+'" without open "'+o+'"'},hint:{[t+"_close"]:null==i?null:`\nThe ${t} directive must start with the characters "${o}" and end\nwith the characters "${i}". The end characters "${i}" may not\nappear without the start characters "${o}" appearing first:\n"${o}...${i}".\n`}});let a=e.token.CA;c=e.fixed(o),f=null==i?null:e.fixed(i),l.forEach(n=>{e.rule(n,e=>(e.open({s:[c],p:t,n:{dr:1}}),null!=i&&(e.open([{s:[f],c:{n:{dr:0}},e:(e,n)=>n.t0.bad(t+"_close")},{s:[f],b:1}]),e.close({s:[f],b:1})),e))}),e.rule(t,e=>e.clear().bo(e=>e.node={}).open([{p:"val",n:null==i?{}:{pk:-1,il:0}}]).bc((...e)=>r(...e)).close(null!=i?[{s:[f]},{s:[a,f]}]:[]))};return e.Directive=n,n.defaults={rules:"val,pair,elem"},e})); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).JsonicDirective=e()}}((function(){var e={};Object.defineProperty(e,"__esModule",{value:!0}),e.Directive=void 0;const n=(e,n)=>{let l,t=("string"==typeof n.rules?n.rules.split(/\s*,\s*/):n.rules||[]).filter(e=>""!==e),o=n.name,i=n.open,r=n.close;if("string"==typeof n.action){let t=n.action;l=n=>n.node=e.util.prop(e.options,t)}else l=n.action;let s={},u="#D_open_"+o,c="#D_close_"+o,d=e.fixed(i),f=null==r?null:e.fixed(r);if(null!=d)throw new Error("Directive open token already in use: "+i);s[u]=i,null==f&&null!=r&&(s[c]=r),e.options({fixed:{token:s},error:{[o+"_close"]:null==r?null:"directive "+o+' close "'+r+'" without open "'+i+'"'},hint:{[o+"_close"]:null==r?null:`\nThe ${o} directive must start with the characters "${i}" and end\nwith the characters "${r}". The end characters "${r}" may not\nappear without the start characters "${i}" appearing first:\n"${i}...${r}".\n`}});let a=e.token.CA;d=e.fixed(i),f=null==r?null:e.fixed(r),t.forEach(n=>{e.rule(n,e=>(e.open({s:[d],p:o,n:{dr:1}}),null!=r&&(e.open([{s:[f],c:{n:{dr:0}},e:(e,n)=>n.t0.bad(o+"_close")},{s:[f],b:1}]),e.close({s:[f],b:1})),e))}),e.rule(o,e=>e.clear().bo(e=>{e.node={}}).open([{p:"val",n:null==r?{}:{pk:-1,il:0}}]).bc((function(e,n){let t=l.call(this,e,n);if(null==t?void 0:t.isToken)return t})).close(null!=r?[{s:[f]},{s:[a,f]}]:[]))};return e.Directive=n,n.defaults={rules:"val,pair,elem"},e})); |
@@ -1,4 +0,11 @@ | ||
/* Copyright (c) 2021 Richard Rodger, MIT License */ | ||
/* Copyright (c) 2021-2022 Richard Rodger, MIT License */ | ||
import { Jsonic, Rule, RuleSpec, AltAction, Plugin } from '@jsonic/jsonic-next' | ||
import { | ||
Jsonic, | ||
Rule, | ||
RuleSpec, | ||
StateAction, | ||
Plugin, | ||
Context, | ||
} from '@jsonic/jsonic-next' | ||
@@ -8,3 +15,3 @@ type DirectiveOptions = { | ||
open: string | ||
action: AltAction | string | ||
action: StateAction | string | ||
close?: string | ||
@@ -23,8 +30,10 @@ rules?: string | string[] | ||
let close = options.close | ||
let action = options.action | ||
let action: StateAction | ||
if ('string' === typeof action) { | ||
let path = action | ||
if ('string' === typeof options.action) { | ||
let path = options.action | ||
action = (rule: Rule) => | ||
(rule.node = jsonic.util.prop(jsonic.options, path)) | ||
} else { | ||
action = options.action | ||
} | ||
@@ -117,3 +126,3 @@ | ||
.clear() | ||
.bo((rule: Rule) => (rule.node = {})) | ||
.bo((rule: Rule) => ((rule.node = {}), undefined)) | ||
.open([ | ||
@@ -128,3 +137,9 @@ { | ||
]) | ||
.bc((...all: any[]) => (action as any)(...all)) | ||
// .bc((...all: any[]) => (action as any)(...all)) | ||
.bc(function (this: RuleSpec, rule: Rule, ctx: Context) { | ||
let out = action.call(this, rule, ctx) | ||
if (out?.isToken) { | ||
return out | ||
} | ||
}) | ||
.close(null != close ? [{ s: [CLOSE] }, { s: [CA, CLOSE] }] : []) | ||
@@ -131,0 +146,0 @@ ) |
{ | ||
"name": "@jsonic/directive", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "This plugin allows the [Jsonic](https://jsonic.senecajs.org) JSON parser to support directive syntax.", | ||
@@ -45,6 +45,6 @@ "main": "directive.js", | ||
"browserify": "^17.0.0", | ||
"esbuild": "^0.14.40", | ||
"esbuild": "^0.14.42", | ||
"esbuild-jest": "^0.5.0", | ||
"jest": "^28.1.0", | ||
"@jsonic/jsonic-next": "2.0.1", | ||
"@jsonic/jsonic-next": "2.0.2", | ||
"prettier": "^2.6.2", | ||
@@ -51,0 +51,0 @@ "tinyify": "^3.0.0", |
Sorry, the diff of this file is not supported yet
16144
260