amocrm-client
Version:
JS Library for AmoCRM
87 lines • 2.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResourceCollection = void 0;
class ResourceCollection {
constructor(data) {
this.data = data || [];
}
get(id) {
return this.data.find((element) => element.hasOwnProperty('id') && element.id === id);
}
map(callbackfn) {
return new ResourceCollection(this.data.map(callbackfn));
}
filter(callbackfn) {
return new ResourceCollection(this.data.filter(callbackfn));
}
reduce(callbackfn, initialValue) {
return this.data.reduce(callbackfn, initialValue);
}
find(callbackfn) {
return this.data.find(callbackfn);
}
findWhere(props) {
return this.data.find((element) => {
return Object.entries(props).every(([key, value]) => {
return element[key] === value;
});
});
}
where(props) {
return this.filter((element) => {
return Object.entries(props).every(([key, value]) => {
return element[key] === value;
});
});
}
every(callbackfn) {
return this.data.every(callbackfn);
}
some(callbackfn) {
return this.data.some(callbackfn);
}
each(callbackfn) {
this.data.forEach(callbackfn);
}
chunk(size) {
const chunks = [];
for (let i = 0, j = this.data.length; i < j; i += size) {
chunks.push(this.data.slice(i, i + size));
}
return new ResourceCollection(chunks);
}
add(models) {
if (models instanceof ResourceCollection) {
this.data.push(...models.data);
}
else {
this.data.push(...models);
}
}
push(model) {
this.data.push(model);
}
pluck(key) {
return this.data.map((model) => model[key]);
}
sort(compareFn) {
return new ResourceCollection(this.data.sort(compareFn));
}
sortBy(key) {
return new ResourceCollection(this.data.sort((a, b) => (a[key] > b[key] ? 1 : -1)));
}
first() {
return this.data[0];
}
last() {
return this.data[this.data.length - 1];
}
all() {
return this.data;
}
size() {
return this.data.length;
}
}
exports.ResourceCollection = ResourceCollection;
//# sourceMappingURL=ResourceCollection.js.map