ember-ast-helpers
Advanced tools
@@ -6,2 +6,3 @@ import { AST } from '@glimmer/syntax'; | ||
skipIfMissing?: boolean; | ||
skipIfMissingDynamic?: boolean; | ||
callback?: (interpolations: { | ||
@@ -11,2 +12,2 @@ [key: string]: any; | ||
}; | ||
export default function interpolateProperties(interpolation: string, {divisor, skipIfMissing, callback}?: interpolatePropertiesOptions): (this: BuildTimeComponent) => string | AST.MustacheStatement | AST.TextNode | AST.PathExpression | AST.SubExpression | AST.StringLiteral | AST.BooleanLiteral | AST.NumberLiteral | AST.UndefinedLiteral | AST.NullLiteral | AST.ConcatStatement | undefined; | ||
export default function interpolateProperties(interpolation: string, {divisor, skipIfMissing, skipIfMissingDynamic, callback}?: interpolatePropertiesOptions): (this: BuildTimeComponent) => string | AST.MustacheStatement | AST.TextNode | AST.PathExpression | AST.SubExpression | AST.StringLiteral | AST.BooleanLiteral | AST.NumberLiteral | AST.UndefinedLiteral | AST.NullLiteral | AST.ConcatStatement | undefined; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const syntax_1 = require("@glimmer/syntax"); | ||
const html_1 = require("../html"); | ||
@@ -23,3 +24,7 @@ function splitInterpolation(interpolation, divisor) { | ||
} | ||
function interpolateProperties(interpolation, { divisor = ':', skipIfMissing = true, callback } = {}) { | ||
function buildConcatIfPresent(cond, concatParts) { | ||
let sanitizedConcatParts = concatParts.map((p) => typeof p === 'string' ? syntax_1.builders.string(p) : p); | ||
return syntax_1.builders.mustache(syntax_1.builders.path('if'), [cond, syntax_1.builders.sexpr(syntax_1.builders.path('concat'), sanitizedConcatParts)]); | ||
} | ||
function interpolateProperties(interpolation, { divisor = ':', skipIfMissing = true, skipIfMissingDynamic = false, callback } = {}) { | ||
let parts = splitInterpolation(interpolation, divisor); | ||
@@ -65,2 +70,17 @@ return function _interpolate() { | ||
} | ||
if (skipIfMissingDynamic) { | ||
let interpolationsArray = []; | ||
for (let key in interpolations) { | ||
interpolationsArray.push(interpolations[key]); | ||
} | ||
if (interpolationsArray.length > 1) { | ||
throw new Error("Can't pass `skipIfMissingDynamic: true` with more than one interpolated value"); | ||
} | ||
let interpolation = interpolationsArray[0]; | ||
if (typeof interpolation === 'object') { | ||
if (interpolation.type === 'PathExpression' || interpolation.type === 'SubExpression') { | ||
return buildConcatIfPresent(interpolation, concatParts); | ||
} | ||
} | ||
} | ||
return html_1.buildAttrContent(concatParts); | ||
@@ -67,0 +87,0 @@ } |
{ | ||
"name": "ember-ast-helpers", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"description": "Utility belt to level-up your Ember AST transforms", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
33461
3.81%724
2.99%