UNPKG

@future-widget-lab/record-set

Version:

A dedicated data structure for in-memory record collections, offering fluent, immutable APIs for MongoDB-like querying, sorting, and transformation.

3 lines (2 loc) 8.14 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("mingo"),e=require("sift");function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=Array(e);n<e;n++)t[n]=r[n];return t}function t(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function o(){return o=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)({}).hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},o.apply(null,arguments)}var s=function(r){var e;return null!=(e=r.records[0])?e:null},c=function(r){var e=r.count,n=r.records;return e<=0?n:n.slice(e)},u=function(r){var e=r.count;return e<0||0===e?[]:r.records.slice(0,e)},i=function(r){for(var e,n=r.fields,s=[],c=t(r.records);!(e=c()).done;){for(var u,i=o({},e.value),a=t(n);!(u=a()).done;){var d=u.value;d in i&&delete i[d]}s.push(i)}return s},a=function(r){for(var e,n=r.fields,o=[],s=t(r.records);!(e=s()).done;){for(var c,u=e.value,i={},a=t(n);!(c=a()).done;){var d=c.value;d in u&&(i[d]=u[d])}o.push(i)}return o},d=function(r){var e=r.newRecords,n=r.index,t=r.records,o=Array.isArray(e)?e:[e],s="number"==typeof n&&n>=0&&n<=t.length?n:t.length;return[].concat(t.slice(0,s),o,t.slice(s))};exports.RecordSet=function(){function n(r){if(void 0===r&&(r=[]),this.records=void 0,!Array.isArray(r))throw new Error('A record set records must be an array. You are seeing this error because a type of "'+typeof r+'" was passed to the record set constructor.');this.records=r||[];for(var e,n=t(Object.getOwnPropertyNames(Object.getPrototypeOf(this)));!(e=n()).done;){var o=e.value,s=this[o];"constructor"!==o&&"function"==typeof s&&(this[o]=s.bind(this))}}n.of=function(r){return new n(r)},n.empty=function(){return new n};var f=n.prototype;return f[Symbol.iterator]=function(){return this.records[Symbol.iterator]()},f.all=function(){return[].concat(this.records)},f.at=function(r){return null!=(n=(e={index:r,records:this.records}).records.at(e.index))?n:null;var e,n},f.first=function(){return s({records:this.records})},f.last=function(){return null!=(r=(e=this.records)[e.length-1])?r:null;var r,e},f.skip=function(r){return new n(c({count:r,records:this.records}))},f.limit=function(r){return new n(u({count:r,records:this.records}))},f.page=function(r,e){return new n(function(r){var e=r.pageNumber,n=r.pageSize;return e<1||n<1?[]:u({count:n,records:c({count:(e-1)*n,records:r.records})})}({pageNumber:r,pageSize:e,records:this.records}))},f.length=function(){return this.records.length},f.isEmpty=function(){return 0===this.records.length},f.find=function(r){return n.of(function(r){var n=r.query,t=r.records;return n?t.filter(e(n)):t}({query:r,records:this.records}))},f.findIndex=function(r){return(n={query:r,records:this.records}).records.findIndex(e(n.query));var n},f.findOne=function(r){return function(r){var n,t=r.query,o=r.records;return t?null!=(n=o.find(e(t)))?n:null:s({records:o})}({query:r,records:this.records})},f.count=function(r){return function(r){var n=r.query,t=r.records;return n?t.filter(e(n)).length:t.length}({query:r,records:this.records})},f.exists=function(r){return function(r){var n=r.query,t=r.records;return n?t.some(e(n)):t.length>0}({query:r,records:this.records})},f.every=function(r){return(n={query:r,records:this.records}).records.every(e(n.query));var n},f.none=function(r){return!(n={query:r,records:this.records}).records.some(e(n.query));var n},f.distinct=function(r,n){return function(r){for(var n,o=r.field,s=r.query,c=r.records,u=s?c.filter(e(s)):c,i=new Set,a=[],d=t(u);!(n=d()).done;){var f=n.value[o];i.has(f)||(i.add(f),a.push(f))}return a}({field:r,query:n,records:this.records})},f.forEach=function(r){this.records.forEach(r)},f.map=function(r){return new n(function(r){for(var e,n=r.transformer,o=[],s=t(r.records);!(e=s()).done;)o.push(n(e.value));return o}({transformer:r,records:this.records}))},f.flatMap=function(r){return new n(function(r){for(var e,n=r.transformer,o=[],s=t(r.records);!(e=s()).done;)for(var c,u=t(n(e.value));!(c=u()).done;)o.push(c.value);return o}({transformer:r,records:this.records}))},f.slice=function(r,e){return new n((t={start:r,end:e,records:this.records}).records.slice(t.start,t.end));var t},f.reduce=function(r,e){return function(r){for(var e,n=r.reducer,o=r.initialValue,s=t(r.records);!(e=s()).done;)o=n(o,e.value);return o}({reducer:r,initialValue:e,records:this.records})},f.concat=function(r){return new n([].concat(this.records,r.all()))},f.pluck=function(r){return function(r){for(var e,n=r.field,o=[],s=t(r.records);!(e=s()).done;)o.push(e.value[n]);return o}({field:r,records:this.records})},f.pick=function(r){return n.of(a({fields:r,records:this.records}))},f.omit=function(r){return n.of(i({fields:r,records:this.records}))},f.select=function(r){return new n(function(r){var e=r.spec,n=r.records,o=[],s=[];if("object"!=typeof e||Array.isArray(e))for(var c,u=t("string"==typeof e?e.split(/\s+/).filter(Boolean):e);!(c=u()).done;){var d=c.value;d.startsWith("-")?s.push(d.slice(1)):d.startsWith("+")?o.push(d.slice(1)):o.push(d)}else{var f=Object.keys(e);f.some((function(r){return 1===e[r]}))?o=f.filter((function(r){return 1===e[r]})):s=f.filter((function(r){return 0===e[r]}))}return o.length>0?a({fields:o,records:n}):i({fields:s,records:n})}({spec:r,records:this.records}))},f.sort=function(r){return new n(function(r){var e=r.compareFn;return[].concat(r.records).sort(e)}({compareFn:r,records:this.records}))},f.sortBy=function(r,e){return new n(function(r){var e=r.iteratees,n=r.orders,t=r.records,o=Array.isArray(e)?e:[e],s=void 0===n?o.map((function(){return"asc"})):Array.isArray(n)?n:[n];return[].concat(t).sort((function(r,e){for(var n=0;n<o.length;n++){var t,c=o[n],u=null!=(t=s[n])?t:"asc",i=r[c],a=e[c];if(i!==a){var d="asc"===u?1:-1;return"string"==typeof i&&"string"==typeof a?i.localeCompare(a)*d:(i<a?-1:1)*d}}return 0}))}({iteratees:r,orders:e,records:this.records}))},f.groupBy=function(r){for(var e,o=function(r){for(var e,n=r.keyExtractor,o=r.records,s=new Map,c=t(o);!(e=c()).done;){var u=e.value,i=n(u),a=s.get(i);a?a.push(u):s.set(i,[u])}return s}({keyExtractor:r,records:this.records}),s=new Map,c=t(o);!(e=c()).done;){var u=e.value;s.set(u[0],new n(u[1]))}return s},f.reverse=function(){return new n([].concat(this.records).reverse())},f.query=function(e,n){return new r.Query(e,n).find(this.records)},f.add=function(r,e){return new n(d({newRecords:r,index:e,records:this.records}))},f.prepend=function(r){return new n(d({newRecords:r,index:0,records:this.records}))},f.append=function(r){return new n(d({newRecords:r,records:this.records}))},f.update=function(r,s){return new n(function(r){for(var n,s=r.records,c=r.changes,u=e(r.query),i=[],a=t(s);!(n=a()).done;){var d=n.value;u(d)?i.push(o({},d,c)):i.push(d)}return i}({query:r,changes:s,records:this.records}))},f.updateOne=function(r,s){return new n(function(r){for(var n,s=r.records,c=r.changes,u=e(r.query),i=[].concat(s),a=0,d=t(i);!(n=d()).done;){var f=n.value;if(u(f)){i[a]=o({},f,c);break}a++}return i}({query:r,changes:s,records:this.records}))},f.remove=function(r){return new n(function(r){for(var n,o=r.records,s=e(r.query),c=[],u=t(o);!(n=u()).done;){var i=n.value;s(i)||c.push(i)}return c}({query:r,records:this.records}))},f.removeOne=function(r){return new n(function(r){for(var n,o=r.records,s=e(r.query),c=[],u=!1,i=t(o);!(n=i()).done;){var a=n.value;u||!s(a)?c.push(a):u=!0}return c}({query:r,records:this.records}))},n}(); //# sourceMappingURL=record-set.cjs.production.min.js.map