hbs-dedent-helper
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -9,2 +9,5 @@ 'use strict'; | ||
var assert = _interopDefault(require('assert')); | ||
var tsPattern = require('ts-pattern'); | ||
var repeat = _interopDefault(require('lodash.repeat')); | ||
var isEmpty = _interopDefault(require('lodash.isempty')); | ||
@@ -14,20 +17,12 @@ exports.levelSize = 4; | ||
function buildMatcherRegex(count, unit) { | ||
switch (unit) { | ||
case 'level': | ||
case 'levels': | ||
return exports.levelChar + "{" + count * exports.levelSize + "}"; | ||
var buildMatcherRegex = function buildMatcherRegex(count, unit) { | ||
return tsPattern.match(unit)["with"]('level', 'levels', function () { | ||
return exports.levelChar + "{" + count * exports.levelSize + "}"; | ||
})["with"]('tab', 'tabs', function () { | ||
return "\\t{" + count + "}"; | ||
})["with"]('space', 'spaces', function () { | ||
return "[ ]{" + count + "}"; | ||
}).exhaustive(); | ||
}; | ||
case 'tab': | ||
case 'tabs': | ||
return "\\t{" + count + "}"; | ||
case 'space': | ||
case 'spaces': | ||
return "[ ]{" + count + "}"; | ||
} | ||
throw new Error("Invalid unit specified: " + unit); | ||
} | ||
function dedentHelper(count, unit, options) { | ||
@@ -42,7 +37,39 @@ var innerContent = options.fn(this); | ||
} | ||
var convertToRaw = function convertToRaw(count, unit) { | ||
return repeat(tsPattern.match(unit)["with"]('level', 'levels', function () { | ||
return exports.levelChar; | ||
})["with"]('tab', 'tabs', function () { | ||
return '\t'; | ||
})["with"]('space', 'spaces', function () { | ||
return ' '; | ||
}).exhaustive(), count); | ||
}; | ||
function baseIndentHelper(count, unit, options) { | ||
var innerContent = options.fn(this); | ||
var lines = innerContent.split('\n'); | ||
var indents = lines.filter(function (line) { | ||
return !isEmpty(line); | ||
}).map(function (line) { | ||
var _line$match$1$length, _line$match, _line$match$; | ||
return (_line$match$1$length = (_line$match = line.match(/^(\s*)/)) == null ? void 0 : (_line$match$ = _line$match[1]) == null ? void 0 : _line$match$.length) != null ? _line$match$1$length : 0; | ||
}); | ||
var minIndent = Math.min.apply(Math, indents); | ||
var minIndentRegex = new RegExp("^\\s{" + minIndent + "}"); | ||
var rawBaseIndent = convertToRaw(count, unit); | ||
return lines.map(function (line) { | ||
return line.replace(minIndentRegex, rawBaseIndent); | ||
}).join('\n'); | ||
} | ||
function trimTrailingWSHelper(options) { | ||
return options.fn(this).split('\n').map(function (it) { | ||
return it.replace(/\s+$/, ''); | ||
}).join('\n'); | ||
} | ||
function register() { | ||
Handlebars.registerHelper('dedent-by', function dedentBy(count, unit, options) { | ||
assert(typeof count === 'number', 'Expected count to be a number'); | ||
assert(typeof unit === 'string', 'Expected unit to be a string'); | ||
return dedentHelper.apply(this, [count, unit, options]); | ||
return dedentHelper.apply(this, [count, unit || 'level', options]); | ||
}); | ||
@@ -52,2 +79,9 @@ Handlebars.registerHelper('dedent', function dedent(options) { | ||
}); | ||
Handlebars.registerHelper('base-indent', function baseIndent(count, unit, options) { | ||
assert(typeof count === 'number', 'Expected count to be a number'); | ||
return baseIndentHelper.apply(this, [count, unit || 'level', options]); | ||
}); | ||
Handlebars.registerHelper('trim-trailing-whitespace', function trimTrailingWS(options) { | ||
return trimTrailingWSHelper.apply(this, [options]); | ||
}); | ||
} | ||
@@ -61,2 +95,3 @@ function setLevelSize(size) { | ||
exports.baseIndentHelper = baseIndentHelper; | ||
exports.dedentHelper = dedentHelper; | ||
@@ -66,2 +101,3 @@ exports.register = register; | ||
exports.setLevelSize = setLevelSize; | ||
exports.trimTrailingWSHelper = trimTrailingWSHelper; | ||
//# sourceMappingURL=hbs-dedent-helper.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("handlebars"),r=(e=require("assert"))&&"object"==typeof e&&"default"in e?e.default:e;function s(e,t,r){var s=r.fn(this),n=function(e,t){switch(t){case"level":case"levels":return exports.levelChar+"{"+e*exports.levelSize+"}";case"tab":case"tabs":return"\\t{"+e+"}";case"space":case"spaces":return"[ ]{"+e+"}"}throw new Error("Invalid unit specified: "+t)}(e,t);return s.split("\n").map((function(e){var t=e.match(new RegExp("^(\\s*)("+n+")[^\\s]"));return t?""+t[1]+e.slice(t[0].length-1):e})).join("\n")}exports.levelSize=4,exports.levelChar="\\s",exports.dedentHelper=s,exports.register=function(){t.registerHelper("dedent-by",(function(e,t,n){return r("number"==typeof e,"Expected count to be a number"),r("string"==typeof t,"Expected unit to be a string"),s.apply(this,[e,t,n])})),t.registerHelper("dedent",(function(e){return s.apply(this,[1,"level",e])}))},exports.setLevelChar=function(e){exports.levelChar=e},exports.setLevelSize=function(e){exports.levelSize=e}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("handlebars"),r=e(require("assert")),n=require("ts-pattern"),i=e(require("lodash.repeat")),l=e(require("lodash.isempty"));function u(e,t,r){var i=r.fn(this),l=function(e,t){return n.match(t).with("level","levels",(function(){return exports.levelChar+"{"+e*exports.levelSize+"}"})).with("tab","tabs",(function(){return"\\t{"+e+"}"})).with("space","spaces",(function(){return"[ ]{"+e+"}"})).exhaustive()}(e,t);return i.split("\n").map((function(e){var t=e.match(new RegExp("^(\\s*)("+l+")[^\\s]"));return t?""+t[1]+e.slice(t[0].length-1):e})).join("\n")}function s(e,t,r){var u=r.fn(this).split("\n"),s=u.filter((function(e){return!l(e)})).map((function(e){var t,r,n;return null!=(t=null==(r=e.match(/^(\s*)/))||null==(n=r[1])?void 0:n.length)?t:0})),p=Math.min.apply(Math,s),a=new RegExp("^\\s{"+p+"}"),o=function(e,t){return i(n.match(t).with("level","levels",(function(){return exports.levelChar})).with("tab","tabs",(function(){return"\t"})).with("space","spaces",(function(){return" "})).exhaustive(),e)}(e,t);return u.map((function(e){return e.replace(a,o)})).join("\n")}function p(e){return e.fn(this).split("\n").map((function(e){return e.replace(/\s+$/,"")})).join("\n")}exports.levelSize=4,exports.levelChar="\\s",exports.baseIndentHelper=s,exports.dedentHelper=u,exports.register=function(){t.registerHelper("dedent-by",(function(e,t,n){return r("number"==typeof e,"Expected count to be a number"),u.apply(this,[e,t||"level",n])})),t.registerHelper("dedent",(function(e){return u.apply(this,[1,"level",e])})),t.registerHelper("base-indent",(function(e,t,n){return r("number"==typeof e,"Expected count to be a number"),s.apply(this,[e,t||"level",n])})),t.registerHelper("trim-trailing-whitespace",(function(e){return p.apply(this,[e])}))},exports.setLevelChar=function(e){exports.levelChar=e},exports.setLevelSize=function(e){exports.levelSize=e},exports.trimTrailingWSHelper=p; | ||
//# sourceMappingURL=hbs-dedent-helper.cjs.production.min.js.map |
import { registerHelper } from 'handlebars'; | ||
import assert from 'assert'; | ||
import { match } from 'ts-pattern'; | ||
import repeat from 'lodash.repeat'; | ||
import isEmpty from 'lodash.isempty'; | ||
@@ -7,20 +10,12 @@ var levelSize = 4; | ||
function buildMatcherRegex(count, unit) { | ||
switch (unit) { | ||
case 'level': | ||
case 'levels': | ||
return levelChar + "{" + count * levelSize + "}"; | ||
var buildMatcherRegex = function buildMatcherRegex(count, unit) { | ||
return match(unit)["with"]('level', 'levels', function () { | ||
return levelChar + "{" + count * levelSize + "}"; | ||
})["with"]('tab', 'tabs', function () { | ||
return "\\t{" + count + "}"; | ||
})["with"]('space', 'spaces', function () { | ||
return "[ ]{" + count + "}"; | ||
}).exhaustive(); | ||
}; | ||
case 'tab': | ||
case 'tabs': | ||
return "\\t{" + count + "}"; | ||
case 'space': | ||
case 'spaces': | ||
return "[ ]{" + count + "}"; | ||
} | ||
throw new Error("Invalid unit specified: " + unit); | ||
} | ||
function dedentHelper(count, unit, options) { | ||
@@ -35,7 +30,39 @@ var innerContent = options.fn(this); | ||
} | ||
var convertToRaw = function convertToRaw(count, unit) { | ||
return repeat(match(unit)["with"]('level', 'levels', function () { | ||
return levelChar; | ||
})["with"]('tab', 'tabs', function () { | ||
return '\t'; | ||
})["with"]('space', 'spaces', function () { | ||
return ' '; | ||
}).exhaustive(), count); | ||
}; | ||
function baseIndentHelper(count, unit, options) { | ||
var innerContent = options.fn(this); | ||
var lines = innerContent.split('\n'); | ||
var indents = lines.filter(function (line) { | ||
return !isEmpty(line); | ||
}).map(function (line) { | ||
var _line$match$1$length, _line$match, _line$match$; | ||
return (_line$match$1$length = (_line$match = line.match(/^(\s*)/)) == null ? void 0 : (_line$match$ = _line$match[1]) == null ? void 0 : _line$match$.length) != null ? _line$match$1$length : 0; | ||
}); | ||
var minIndent = Math.min.apply(Math, indents); | ||
var minIndentRegex = new RegExp("^\\s{" + minIndent + "}"); | ||
var rawBaseIndent = convertToRaw(count, unit); | ||
return lines.map(function (line) { | ||
return line.replace(minIndentRegex, rawBaseIndent); | ||
}).join('\n'); | ||
} | ||
function trimTrailingWSHelper(options) { | ||
return options.fn(this).split('\n').map(function (it) { | ||
return it.replace(/\s+$/, ''); | ||
}).join('\n'); | ||
} | ||
function register() { | ||
registerHelper('dedent-by', function dedentBy(count, unit, options) { | ||
assert(typeof count === 'number', 'Expected count to be a number'); | ||
assert(typeof unit === 'string', 'Expected unit to be a string'); | ||
return dedentHelper.apply(this, [count, unit, options]); | ||
return dedentHelper.apply(this, [count, unit || 'level', options]); | ||
}); | ||
@@ -45,2 +72,9 @@ registerHelper('dedent', function dedent(options) { | ||
}); | ||
registerHelper('base-indent', function baseIndent(count, unit, options) { | ||
assert(typeof count === 'number', 'Expected count to be a number'); | ||
return baseIndentHelper.apply(this, [count, unit || 'level', options]); | ||
}); | ||
registerHelper('trim-trailing-whitespace', function trimTrailingWS(options) { | ||
return trimTrailingWSHelper.apply(this, [options]); | ||
}); | ||
} | ||
@@ -54,3 +88,3 @@ function setLevelSize(size) { | ||
export { dedentHelper, levelChar, levelSize, register, setLevelChar, setLevelSize }; | ||
export { baseIndentHelper, dedentHelper, levelChar, levelSize, register, setLevelChar, setLevelSize, trimTrailingWSHelper }; | ||
//# sourceMappingURL=hbs-dedent-helper.esm.js.map |
import * as Handlebars from 'handlebars'; | ||
export declare let levelSize: number; | ||
export declare let levelChar: string; | ||
export declare function dedentHelper(this: any, count: number, unit: string, options: Handlebars.HelperOptions): string; | ||
declare type Unit = 'level' | 'levels' | 'tab' | 'tabs' | 'space' | 'spaces'; | ||
export declare function dedentHelper(this: any, count: number, unit: Unit, options: Handlebars.HelperOptions): string; | ||
export declare function baseIndentHelper(this: any, count: number, unit: Unit, options: Handlebars.HelperOptions): string; | ||
export declare function trimTrailingWSHelper(this: any, options: Handlebars.HelperOptions): string; | ||
export declare function register(): void; | ||
export declare function setLevelSize(size: number): void; | ||
export declare function setLevelChar(char: string): void; | ||
export {}; |
{ | ||
"name": "hbs-dedent-helper", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"main": "dist/index.js", | ||
@@ -36,2 +36,4 @@ "module": "dist/hbs-dedent-helper.esm.js", | ||
"@types/jest": "^28.1.1", | ||
"@types/lodash.isempty": "^4.4.7", | ||
"@types/lodash.repeat": "^4.1.7", | ||
"@types/node": "^17.0.42", | ||
@@ -46,2 +48,7 @@ "handlebars": "^4.7.7", | ||
}, | ||
"dependencies": { | ||
"lodash.isempty": "^4.4.0", | ||
"lodash.repeat": "^4.1.0", | ||
"ts-pattern": "^4.0.3" | ||
}, | ||
"scripts": { | ||
@@ -48,0 +55,0 @@ "start": "tsdx watch", |
# Handlebars dedent helper | ||
A simple handlebars helper to dedent blocks of text in handlebars. | ||
A set of simple handlebars helpers to help with indentation | ||
@@ -26,3 +26,3 @@ Very often, when using block helpers our code would end up nested more than it should be. Which can be problematic for code-generators: | ||
To solve this we provide two helpers: | ||
To solve this we provide following helpers: | ||
@@ -87,2 +87,28 @@ 1. `dedent` helper: | ||
3. `base-indent` helper: | ||
(Changes the minimum indentation of all lines in the contained block, while retaining relative indentation) | ||
``` | ||
if (foo) { | ||
{{#base-indent 1 "level"}} | ||
{{#each items}} | ||
console.log('{{.}}') | ||
{{/each}} | ||
{{/base-indent}} | ||
} | ||
``` | ||
``` | ||
if (foo) { | ||
console.log(1) | ||
} | ||
``` | ||
This is particularly useful when including other templates in a base template. | ||
4. `trim-trailing-whitespace` helper: | ||
(Removes all trailing whitespace in generated output) | ||
# Installation | ||
@@ -89,0 +115,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32591
192
124
4
11
+ Addedlodash.isempty@^4.4.0
+ Addedlodash.repeat@^4.1.0
+ Addedts-pattern@^4.0.3
+ Addedlodash.isempty@4.4.0(transitive)
+ Addedlodash.repeat@4.1.0(transitive)
+ Addedts-pattern@4.3.0(transitive)