UNPKG

@zendesk/laika

Version:

Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!

43 lines 2.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startsWithNumber = exports.forEachDeep = exports.indexOfWord = void 0; const forEach_1 = __importDefault(require("lodash/forEach")); const isObject_1 = __importDefault(require("lodash/isObject")); /** * Like String.indexOf but only matches when surrounded by word boundary */ const indexOfWord = (input, searchedWord, _preceedingChar = ' ', _index = 0) => { const referencedIndex = input.indexOf(searchedWord); if (referencedIndex < 0) return referencedIndex; // poor man's RegExp word boundary (we don't wanna do regexp for such large strings) const preceedingChar = referencedIndex > 0 ? input[referencedIndex - 1] : referencedIndex === 0 ? _preceedingChar : ' '; const matchEndIndex = referencedIndex + searchedWord.length; const followingChar = matchEndIndex < input.length ? input[matchEndIndex] : ' '; const nonWordChar = /\W/; if (nonWordChar.test(preceedingChar) && typeof followingChar === 'string' && nonWordChar.test(followingChar)) { return referencedIndex + _index; } // recursive tail-call loop by cutting out the front of the input string every time // until the indexOf results in -1 return (0, exports.indexOfWord)(input.slice(matchEndIndex), searchedWord, input[matchEndIndex - 1], matchEndIndex); }; exports.indexOfWord = indexOfWord; const forEachDeep = ({ path = [], value, key }, callback) => (0, isObject_1.default)(value) ? void (0, forEach_1.default)(value, // eslint-disable-next-line @typescript-eslint/no-shadow (childValue, key) => void (0, exports.forEachDeep)({ path: [...path, key], key, value: childValue }, callback)) : void callback({ path, value, key }); exports.forEachDeep = forEachDeep; const startsWithNumber = (str) => { var _a; return !Number.isNaN(Number.parseInt((_a = str === null || str === void 0 ? void 0 : str[0]) !== null && _a !== void 0 ? _a : '', 10)); }; exports.startsWithNumber = startsWithNumber; //# sourceMappingURL=codeGeneratorUtils.js.map