Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

edge-parser

Package Overview
Dependencies
Maintainers
1
Versions
92
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

edge-parser

Parser for edge template engine

  • 1.0.16
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
decreased by-19.13%
Maintainers
1
Weekly downloads
 
Created
Source

Edge Parser

travis-image appveyor-image coveralls-image npm-image

This repo is the parser to convert edge templates to a self invoked Javascript function. Later you can invoke this function by providing a context.

Example:

Input

Hello {{ username }}

Output

(function (template, ctx) {
  let out = ''
  out += 'Hello '
  out += `${ctx.escape(ctx.resolve('username'))}`
  out += '\n'
  return out
})(ctx)

Notice of use of ctx in the function body. Parser doesn't provide the implementation of ctx, the runtime of template engine should provide it.

Supported Expressions

The following expressions are supported by the parser.

Identifier

The identifier is wrapped inside ctx.resolve. The resolve method job is to resolve the value.

In following statement username is the identifier

Hello {{ username }}
Literal

A string literal

Hello {{ 'Guest' }}
ArrayExpression

The [1, 2, 3, 4] is an array expression.

Evens are {{
  [1, 2, 3, 4].filter((num) => num % 2 === 0)
}}
ObjectExpression

The { username: 'virk' } is an Object expression

{{ toJSON({ username: 'virk' })  }}
UnaryExpression

Following are examples of UnaryExpression.

{{ typeof(username) }}

{{ !!username }} 
BinaryExpression

Here {{ 2 + 2 }} is the binary expression

{{ 2 + 2 }} = 4
LogicalExpression

Following is the example of LogicalExpression.

{{ username || admin.username }}
MemberExpression
{{ username.toUpperCase() }}
ConditionalExpression
{{ username ? username : 'Guest' }}
CallExpression
{{ upper(username) }}
SequenceExpression

Sequence is not supported in mustache blocks and instead used inside tags. For example:

Everything inside () is a sequence expression.

@component('button', text = 'Submit', type = 'Primary')
TemplateLiteral
{{ Hello `${username}` }}
ArrowFunctionExpression
{{
  users.map((user) => {
    return user.username
  })
}}

Context expectations

Context must have following methods to work with the core parser.

class Context {
  // Resolve value for a key
  resolve(key: string): any
  
  // make input HTML safe
  escape (input: string): string
}

Change log

The change log can be found in the CHANGELOG.md file.

Contributing

Everyone is welcome to contribute. Please go through the following guides, before getting started.

  1. Contributing
  2. Code of conduct

Authors & License

thetutlage and contributors.

MIT License, see the included MIT file.

Keywords

FAQs

Package last updated on 12 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc