UNPKG

3.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DropEmptyObjectAtTheEndOfAnArray = void 0;
4const cdk = require("@aws-cdk/core");
5const object_1 = require("./object");
6/**
7 * A Token object that will drop the last element of an array if it is an empty object
8 *
9 * Necessary to prevent options objects that only contain "region" and "account" keys
10 * that evaluate to "undefined" from showing up in the rendered JSON.
11 */
12class DropEmptyObjectAtTheEndOfAnArray {
13 constructor(value) {
14 this.value = value;
15 this.creationStack = cdk.captureStackTrace();
16 }
17 resolve(context) {
18 context.registerPostProcessor(this);
19 return context.resolve(this.value);
20 }
21 postProcess(o, _context) {
22 if (!Array.isArray(o)) {
23 return o;
24 }
25 const lastEl = o[o.length - 1];
26 if (typeof lastEl === 'object' && lastEl !== null && Object.keys(object_1.dropUndefined(lastEl)).length === 0) {
27 return o.slice(0, o.length - 1);
28 }
29 return o;
30 }
31}
32exports.DropEmptyObjectAtTheEndOfAnArray = DropEmptyObjectAtTheEndOfAnArray;
33//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZHJvcC1lbXB0eS1vYmplY3QtYXQtdGhlLWVuZC1vZi1hbi1hcnJheS10b2tlbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImRyb3AtZW1wdHktb2JqZWN0LWF0LXRoZS1lbmQtb2YtYW4tYXJyYXktdG9rZW4udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEscUNBQXFDO0FBQ3JDLHFDQUF5QztBQUV6Qzs7Ozs7R0FLRztBQUNILE1BQWEsZ0NBQWdDO0lBRzNDLFlBQTZCLEtBQVU7UUFBVixVQUFLLEdBQUwsS0FBSyxDQUFLO1FBQ3JDLElBQUksQ0FBQyxhQUFhLEdBQUcsR0FBRyxDQUFDLGlCQUFpQixFQUFFLENBQUM7S0FDOUM7SUFFTSxPQUFPLENBQUMsT0FBNEI7UUFDekMsT0FBTyxDQ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