UNPKG

@microsoft.azure/autorest.incubator

Version:
213 lines 5.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function includeXDash(dictionary) { return Object.keys(dictionary).filter((v, i, a) => v.startsWith('x-')); } exports.includeXDash = includeXDash; function excludeXDash(dictionary) { return Object.keys(dictionary).filter((v, i, a) => !v.startsWith('x-')); } exports.excludeXDash = excludeXDash; class Dictionary { } exports.Dictionary = Dictionary; function ToDictionary(keys, each) { const result = new Dictionary(); keys.map((v, i, a) => result[v] = each(v)); return result; } exports.ToDictionary = ToDictionary; function CopyDictionary(dictionary, each) { return ToDictionary(excludeXDash(dictionary), each); } exports.CopyDictionary = CopyDictionary; /** returns an Linqable<> for keys in the collection */ function keys(source) { if (Array.isArray(source)) { return linqify(source.keys()); } if (source instanceof Map) { return linqify(source.keys()); } if (source) { return linqify((Object.getOwnPropertyNames(source))); } return linqify([]); } exports.keys = keys; /** returns an Linqable<> for values in the collection */ function values(source) { if (Array.isArray(source)) { return linqify(function* () { for (const v of source) { yield v; } }()); } if (source instanceof Map) { return linqify(source.values()); } if (source) { return linqify(function* () { for (const key of keys(source)) { const value = source[key]; if (typeof value !== 'function') { yield value; } } }()); } return linqify([]); } exports.values = values; /** returns an Linqable<{key,value}> for the Collection */ function items(source) { if (Array.isArray(source)) { return linqify(function* () { for (let i = 0; i < source.length; i++) { yield { key: i, value: source[i] }; } }()); } if (source instanceof Map) { return linqify(function* () { for (const [key, value] of source.entries()) { yield { key, value }; } }()); } if (source) { return linqify(function* () { for (const key of keys(source)) { const value = source[key]; if (typeof value !== 'function') { yield { key, value: source[key] }; } } }()); } return linqify([]); } exports.items = items; function length(source) { if (Array.isArray(source)) { return source.length; } if (source instanceof Map) { return source.size; } return source ? Object.getOwnPropertyNames(source).length : 0; } exports.length = length; function any(predicate) { for (const each of this) { if (predicate(each)) { return true; } } return false; } exports.any = any; function all(predicate) { for (const each of this) { if (!predicate(each)) { return false; } } return true; } exports.all = all; function select(selector) { return linqify(function* () { for (const each of this) { yield selector(each); } }.bind(this)()); } exports.select = select; function selectMany(selector) { return linqify(function* () { for (const each of this) { for (const item of selector(each)) { yield item; } } }.bind(this)()); } exports.selectMany = selectMany; function where(predicate) { return linqify(function* () { for (const each of this) { if (predicate(each)) { yield each; } } }.bind(this)()); } exports.where = where; function selectNonNullable(selector) { return linqify(function* () { for (const each of this) { const value = selector(each); if (value) { yield value; } } }.bind(this)()); } exports.selectNonNullable = selectNonNullable; function nonNullable() { return linqify(function* () { for (const each of this) { if (each) { yield each; } } }.bind(this)()); } exports.nonNullable = nonNullable; function first(predicate) { for (const each of this) { if (!predicate || predicate(each)) { return each; } } return undefined; } exports.first = first; function toArray() { return [...this]; } exports.toArray = toArray; function bifurcate(predicate) { const result = [new Array(), new Array()]; for (const each of this) { result[predicate(each) ? 0 : 1].push(each); } return result; } exports.bifurcate = bifurcate; function distinct(selector) { const hash = new Dictionary(); return linqify(function* () { if (!selector) { selector = i => i; } for (const each of this) { const k = JSON.stringify(selector(each)); if (!hash[k]) { hash[k] = true; yield each; } } }.bind(this)()); } function linqify(iterable) { return Object.defineProperty(iterable, 'linq', { get: () => { return { all: all.bind(iterable), any: any.bind(iterable), bifurcate: bifurcate.bind(iterable), distinct: distinct.bind(iterable), first: first.bind(iterable), select: select.bind(iterable), selectMany: selectMany.bind(iterable), selectNonNullable: selectNonNullable.bind(iterable), toArray: toArray.bind(iterable), where: where.bind(iterable), }; } }); } //# sourceMappingURL=dictionary.js.map