UNPKG

@rxap/ts-morph

Version:

Provides utilities for manipulating TypeScript code using the ts-morph library. It offers a fluent API to add, modify, and remove code elements such as classes, decorators, imports, and properties in both Angular and NestJS projects. This package simplifi

48 lines 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FindArrayElementByObjectProperty = FindArrayElementByObjectProperty; exports.DefaultFindExistingElement = DefaultFindExistingElement; exports.CoerceArrayElement = CoerceArrayElement; const ts_morph_1 = require("ts-morph"); function FindArrayElementByObjectProperty(property, value) { return (e) => { if (e.isKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)) { const p = e.getProperty(property); if (p && p.isKind(ts_morph_1.SyntaxKind.PropertyAssignment)) { const i = p.getInitializerIfKind(ts_morph_1.SyntaxKind.StringLiteral); if (i) { return i.getLiteralText() === value; } } } return false; }; } function DefaultFindExistingElement(element) { return (e) => e.getText().trim() === element; } function CoerceArrayElement(array, element, findExisting, insertAt) { if (!findExisting) { if (typeof element === 'string') { findExisting = DefaultFindExistingElement(element); } else { throw new Error('The findExisting function is required or the element must be a string!'); } } for (const e of array.getElements()) { if (findExisting(e)) { return e; } } if (insertAt) { const index = insertAt(array); if (index < 0) { console.warn('The insertAt function returned a negative index - normalizing to 0'); return array.insertElement(0, element); } return array.insertElement(index, element); } return array.addElement(element); } //# sourceMappingURL=coerce-array-element.js.map