Skip to content

Commit

Permalink
option to return value on having equal sign
Browse files Browse the repository at this point in the history
  • Loading branch information
coderosh committed Aug 6, 2020
1 parent 26c31a8 commit a177873
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ulka-parser",
"version": "0.2.2",
"version": "0.2.3",
"description": "Templating engine for ulka static site generator",
"main": "src/index.js",
"scripts": {
Expand Down
69 changes: 46 additions & 23 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
const vm = require('vm');
const path = require('path');
const fs = require('fs');
const { replaceString, hasEqualSign } = require('./utils');

const defaultOptions = {
base: '',
};

function parser(ulkaTemplate, values = {}, options = defaultOptions) {
return ulkaTemplate
.replace(/\\?{%(.*?)%}/gs, (...args) => {
let jsCode = args[1];

values = {
...values,
// require,
console,
};

if (args[0][0] === '\\' && ulkaTemplate[args[2] - 1] !== '\\')
return args[0].slice(1);

jsCode = jsCode.replace(/(var |let |const )/gs, '');

const result = vm.runInNewContext(jsCode, values);

const codeWithoutString = replaceString(jsCode, '');
const containsEqual = hasEqualSign(codeWithoutString);

return containsEqual ? '' : result || '';
})
.trim();
try {
return ulkaTemplate
.replace(/\\?{%(.*?)%}/gs, (...args) => {
let jsCode = args[1];

values = {
...values,
require: reqPath => {
const rPath = path.join(options.base, reqPath);
if (fs.existsSync(rPath)) return require(rPath);
return require(rPath);
},
console,
};

/*
If first index is equal sign then remove the equal sign
*/
const containsEqualsInFirstIndex = jsCode[0] === '=';
if (containsEqualsInFirstIndex) jsCode = jsCode.substr(1);

/*
- {% sth = "roshan" %}
- \{% sth %} => {% sth %}
- \\{% sth %} => \{% "roshan" %}
*/
if (args[0][0] === '\\' && ulkaTemplate[args[2] - 1] !== '\\')
return args[0].slice(1);

jsCode = jsCode.replace(/(var |let |const )/gs, '');

const result = vm.runInNewContext(jsCode, values);

const codeWithoutString = replaceString(jsCode, '');
const containsEqual = hasEqualSign(codeWithoutString);
const shouldPrintResult = !containsEqual || containsEqualsInFirstIndex;

return !shouldPrintResult ? '' : result || '';
})
.trim();
} catch (e) {
console.log(`Ulka Praser Error: `, e.message);
throw e;
}
}

module.exports = parser;
8 changes: 8 additions & 0 deletions tests/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ describe('parse funcion', () => {
);
});
});

describe('given a statement with assignment and equal tags', () => {
test('should return roshan acharya', () => {
expect(parse(`{%= const name = "Roshan Acharya" %}`)).toBe(
'Roshan Acharya',
);
});
});
});

0 comments on commit a177873

Please sign in to comment.