Lodash Template JavaScript Parser

1.1.1 · active · verified Tue Apr 21

This package provides a JavaScript parser/splitter specifically designed for `lodash.template` content. It's currently at version 1.1.1 and appears to be in an active maintenance state, with recent bug fixes and minor feature additions. Its core functionality is to accurately separate JavaScript code segments from template strings, ensuring that the original positional information of the JavaScript code is preserved. A key differentiator is its minimalist approach: unlike more comprehensive tools such as `eslint-plugin-lodash-template`, this library does not include its own JavaScript parser (e.g., espree or babel/parser). This makes it a lightweight solution for use cases like linting tools that need to extract executable JavaScript logic from `lodash` templates for external processing, without requiring a full Abstract Syntax Tree (AST) generation from this package itself.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to parse a lodash template, extract its JavaScript and template content, and apply custom template settings.

import { parseTemplate } from "lodash-template-js-parser";
import assert from 'assert';

const content = `
const age = 18;
<% if (age < 18) { %>
    <li><%= name %> (age: <%= age %>)</li>
<% } else { %>
    <li>over the age limit!</li>
<% }%>`

const { script, template } = parseTemplate(content, {
    templateSettings: {
        interpolate: ["#{", "}"] // Example of custom lodash template settings
    }
});

assert.strictEqual(script, `
               
   if (age < 18) {   
            name ;            age ;   
   } else {   
                                
   }  
`);
assert.strictEqual(template, `
const age = 18;
                     
    <li>            (age:           )</li>
              
    <li>over the age limit!</li>
      
`);

console.log('Script extracted:\n', script);
console.log('Template extracted:\n', template);

view raw JSON →