Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                script(src="https://unpkg.com/petite-vue")
	

main.container.my-3(v-scope="js2jsonlogic()" @mounted="formula();math();logic()")
	.row.my-3
		.col
			div
				label.form-label Formula
				input.form-control.font-monospace(@keyup='formula();math();logic()' v-model="form")
				div.form-text Needs spaces around all operators to be turned into JsonLogic
	.row.my-3
		.col-6
			div
				label.form-label JsonLogic
				textarea.form-control.font-monospace(rows="13" v-model="jsonlogic" readonly="true")
		.col-6
			div
				label.form-label Data
				textarea.form-control.font-monospace(rows="13" @keyup='formula();math();logic()' v-model="data")
	.row.my-3
	
		.col-6
			.input-group
				span.input-group-text json-logic
				input.form-control(v-model="jsonlogicresult" readonly="true")
	
		.col-6
			.input-group
				span.input-group-text math.js
				input.form-control(v-model="mathresult" readonly="true")


              
            
!

CSS

              
                
              
            
!

JS

              
                PetiteVue.createApp().mount()

function js2jsonlogic() {
	return {
		form: this.form || '( label1 + label2 ) * select',
		jsonlogic: '',
		mathresult: '',
		jsonlogicresult: '',
		data: JSON.stringify({
			label1: 1,
			label2: "2",
			select: "3°C"
		},null,'  '),
		formula() {
			this.jsonlogic = JSON.stringify(js2jl(this.form),null,'  ')
		},
		// TODO: calc
		math() {
			try {
				this.mathresult = math.evaluate(this.form, JSON.parse(this.data))
			} catch(e) {
				this.mathresult = e
			}
			
		},
		logic() {
			this.jsonlogicresult = jsonLogic.apply( JSON.parse(this.jsonlogic), JSON.parse(this.data));
		}
	}
		
} 

// @see https://github.com/lucas2595/js2jl
function js2jl(infix) {
  let ops = {
    not: { precedency: 5, single: true },
    "*": { precedency: 4 },
    "/": { precedency: 4 },
    "%": { precedency: 4 },
    "+": { precedency: 3 },
    "-": { precedency: 3 },
    cat: { precedency: 3 },
    in: { precedency: 3 },
    substr: { precedency: 3 },
    "===": { precedency: 2 },
    "!==": { precedency: 2 },
    "!=": { precedency: 2 },
    "==": { precedency: 2 },
    "<": { precedency: 2 },
    ">": { precedency: 2 },
    "<=": { precedency: 2 },
    ">=": { precedency: 2 },
    or: { precedency: 2 },
    and: { precedency: 2 },
    filter: { precedency: 1 },
    map: { precedency: 1 },
    "?": { precedency: 1 },
    reduce: { precedency: 1 },
    method: { precedency: 1 },
    ":": { precedency: 1, helper: "?", key: "if" },
    initial: { precedency: 1, helper: "reduce", key: "reduce" },
    arguments: { precedency: 1, helper: "method", key: "method" }
  };

  let helped = Object.entries(ops)
    .filter(a => a[1].helper)
    .map(a => a[0]);
  let single = Object.entries(ops)
    .filter(a => a[1].single)
    .map(a => a[0]);

  let peek = a => a[a.length - 1];
  let stack = [];
  return infix
    .split(" ")
    .reduce((output, token) => {
      if (token in ops) {
        while (
          peek(stack) in ops &&
          ops[token].precedency <= ops[peek(stack)].precedency
        )
          output.push(stack.pop());
        stack.push(token);
      } else if (token === "(") {
        stack.push(token);
      } else if (token === ")") {
        while (peek(stack) !== "(" && stack.length > 0) {
          output.push(stack.pop());
        }
        stack.pop();
      } else {
        output.push(token);
      }
      return output;
    }, [])
    .concat(stack.reverse())
    .reduce(
      (acc, val) => {
        if (!(val in ops)) {
          if (!isNaN(parseFloat(val))) {
            acc.push(parseFloat(val));
          } else if (
            val !== undefined &&
            (val.split(/"/).length === 3 || val.split(/'/).length === 3)
          ) {
            acc.push(val.replace(/"/g, ""));
          } else if (
            val !== undefined &&
            ((val[0] === "[" && val[val.length - 1] === "]") ||
              (val[0] === "{" && val[val.length - 1] === "}"))
          ) {
            try {
              acc.push(JSON.parse(val));
            } catch (err) {
              acc.push({ var: val });
            }
          } else {
            acc.push({ var: val });
          }
        } else if (helped.includes(val)) {
          // Three arguments
          let a = acc.pop();
          let b = acc.pop();
          if (b && b[ops[val].helper])
            acc = [
              ...acc,
              {
                [ops[val].key]: [
                  b[ops[val].helper][0],
                  b[ops[val].helper][1],
                  a
                ]
              }
            ];
        } else if (single.includes(val)) {
          // Single argument
          let a = acc.pop();
          acc = [...acc, { [val]: a }];
        } else {
          // Two arguments
          let a = acc.pop();
          let b = acc.pop();
          acc = [...acc, { [val]: [b, a] }];
        }
        return acc;
      },
      [{}]
    )
    .pop();
}
              
            
!
999px

Console