obop
Version:
MongoDB-style object operators makes array manipulation easy: where/order/update/view
1 lines • 8.73 kB
JavaScript
var obop=function(){"use strict";function r(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var n,t,e,o;function i(){if(t)return n;function r(){if(!(this instanceof r))return new r}function e(r){for(var n=r.length,t=0;t<n;t++){var e=r[t];if(e instanceof Error)return e}}return t=1,n=r,r.prototype={$gt:function(r){return function(n){return n>r}},$gte:function(r){return function(n){return n>=r}},$in:function(r){if(!(r instanceof Array))return new Error("invalid query");var n=r.length;return function(t){for(var e=0;e<n;e++)if(t==r[e])return!0;return!1}},$lt:function(r){return function(n){return n<r}},$lte:function(r){return function(n){return n<=r}},$ne:function(r){return function(n){return n!=r}},$nin:function(r){var n=r.length;return function(t){for(var e=0;e<n;e++)if(t==r[e])return!1;return!0}},$or:function(r,n){if(r){if(!(r instanceof Array)||0===r.length)return new Error("$or requires nonempty array");var t,o,i=[];return r.forEach((function(r){var t=n.where(r);i.push(t)})),o=(t=i).length,e(t)||function(r){for(var n=0;n<o;n++)if((0,t[n])(r))return!0;return!1}}},$and:function(r,n){if(r){if(!(r instanceof Array)||0===r.length)return new Error("$and expression must be a nonempty array");var t,o,i=[];return r.forEach((function(r){var t=n.where(r);i.push(t)})),o=(t=i).length,e(t)||function(r){for(var n=0;n<o;n++)if(!(0,t[n])(r))return!1;return!0}}},$not:function(r,n){var t=n.where(r);return function(r){return!t(r)}},$exists:function(r){return function(n){return void 0!==n===r}},$size:function(r){return function(n){return void 0!==n&&(n instanceof Array&&n.length===r)}}},n}var f,u={};function a(){if(f)return u;function r(r){var n=this;if("function"==typeof r)return r;if("object"!=typeof(r=r||{}))return new Error("Invalid where operator type: "+r);var t=[],e={},o={},i={};for(var f in r){if(/(^|\.)__proto__(\.|$)/.test(f))return new Error("Invalid target: "+f);var u=r[f],a=f.indexOf("."),c=n.$where[f];if(c){var s=c(u,n,f);s&&t.push(s)}else if(a>-1){var v=f.substr(0,a),h=f.substr(a+1);o[v]=o[v]||{},o[v][h]=u}else{if(u instanceof Array)return new Error("Unknown where operator: "+f);"object"==typeof u?i[f]=u:e[f]=u}}var l,p=Object.keys(e);if(1===p.length){var y=p[0],w=r[y];t.push((function(r){return"object"==typeof r&&r[y]==w}))}else if(p.length>1){t.push((function(r){if("object"!=typeof r)return!1;for(var n in e)if(r[n]!=e[n])return!1;return!0}))}return d(o),d(i),l?new Error(l):t.length<2?t.shift()||null:function(r){var n=r.length;return function(r){for(var n=r.length,t=0;t<n;t++){var e=r[t];if(e instanceof Error)return e}}(r)||function(t){for(var e=0;e<n;e++){if(!(0,r[e])(t))return!1}return!0}}(t);function d(r){Object.keys(r).forEach((function(e){if(!l){var o=r[e],i=n.where(o);if(i)if(i instanceof Error)l=i;else{var f=function(r){return i(r?r[e]:void 0)};f&&t.push(f)}}}))}}return f=1,u.where=function(n,t){var e,o=arguments.length;if(1===o){if((e=r.call(this,n))instanceof Error)throw e;return e}if(2===o){if(n instanceof Array){if((e=r.call(this,t))instanceof Error)throw e;return n.filter(e)}throw new Error("Invalid argument type: "+n)}throw new Error("Invalid arguments length: "+o)},u}var c,s={};var v,h={};var l,p={};var y,w,d={name:"obop",version:"1.0.0"};var b=function(){if(w)return y;w=1;var r=i(),n=function(){if(o)return e;function r(){if(!(this instanceof r))return new r}return o=1,e=r,r.prototype={$set:function(r){return function(n){if("object"!=typeof n)return n;for(var t in r)n[t]=r[t];return n}},$unset:function(r){return function(n){if("object"!=typeof n)return n;for(var t in r)delete n[t];return n}},$rename:function(r){return function(n){if("object"!=typeof n)return n;for(var t in r){var e=n[t];void 0!==e&&(delete n[t],n[r[t]]=e)}return n}},$push:function(r){return function(n){if("object"!=typeof n)return n;for(var t in r){var e=r[t],o=n[t];o instanceof Array||(n[t]=void 0===o?[]:[o]),n[t].push(e)}return n}},$pull:function(r){return function(n){if("object"!=typeof n)return n;for(var t in r){var e=r[t],o=n[t];if(o instanceof Array){for(var i=[],f=o.length,u=0;u<f;u++){var a=o[u];e!=a&&i.push(a)}n[t]=i}else e==o&&(n[t]=[])}return n}},$inc:function(r){return function(n){if("object"!=typeof n)return n;for(var t in r){var e=r[t],o=n[t];e=parseFloat(e)||0,o=parseFloat(o)||0,n[t]=o+e}return n}}},e}();function t(){if(!(this instanceof t))return new t}return t.where=t.prototype.where=a().where,t.view=t.prototype.view=function(){if(c)return s;function r(r){var n=this;if("function"==typeof r)return r;if("object"!=typeof(r=r||{}))return new Error("Invalid view parameters type: "+r);var t=Object.keys(r);if(!t.length)return null;var e,o=t[0],i={},f={},u={},a={};for(var c in r){if(/(^|\.)__proto__(\.|$)/.test(c))return new Error("Invalid target: "+c);var s=r[c],v=c.indexOf(".");if(v>-1){var h=c.substr(0,v),l=c.substr(v+1);e=!0,s?(u[h]=u[h]||{},u[h][l]=s):(a[h]=a[h]||{},a[h][l]=s)}else s?i[c]=!0:f[c]=!0}e&&(Object.keys(u).forEach((function(r){var t=u[r];i[r]=n.view(t)})),Object.keys(a).forEach((function(r){var t=a[r];f[r]=n.view(t)})));var p=Object.keys(i).length,y=Object.keys(f).length;return 1!==p||y||e?p&&!y?e?d:function(r){var n={};return Object.keys(r).forEach((function(t){i[t]&&(n[t]=r[t])})),n}:!p&&y?e?w:function(r){var n={};return Object.keys(r).forEach((function(t){f[t]||(n[t]=r[t])})),n}:function(r){return p&&(r=d(r)),y&&(r=w(r)),r}:function(r){var n={};return r.hasOwnProperty(o)&&(n[o]=r[o]),n};function w(r){var n={};return Object.keys(r).forEach((function(t){var e=f[t],o=r[t];"function"==typeof e?n[t]="object"==typeof o?e(o):o:e||(n[t]=o)})),n}function d(r){var n={};return Object.keys(r).forEach((function(t){var e=i[t],o=r[t];"function"==typeof e?"object"==typeof o&&(n[t]=e(o)):e&&(n[t]=o)})),n}}return c=1,s.view=function(n,t){var e,o=arguments.length;if(1===o){if((e=r.call(this,n))instanceof Error)throw e;return e}if(2===o){if(n instanceof Array){if((e=r.call(this,t))instanceof Error)throw e;return n.map(e)}throw new Error("Invalid argument type: "+n)}throw new Error("Invalid arguments length: "+o)},s}().view,t.order=t.prototype.order=function(){if(v)return h;function r(r){var n=this;if("function"==typeof r)return r;if("object"!=typeof(r=r||{}))return new Error("Invalid order operator type: "+r);if(r instanceof Array){var t;if(r.forEach((function(r){t||r instanceof Array&&2===r.length&&void 0!==r[0]&&r[1]-r[1]==0||(t=new Error("Invalid order pair: "+r))})),t)return t}else{var e=[];for(var o in r){if(/(^|\.)__proto__(\.|$)/.test(o))return new Error("Invalid target: "+o);var i=[o,r[o]];e.push(i)}r=e}var f,u=r.length;if(0===u)return null;for(var a=u-1;a>=0;a--)f=c(r[a][0],r[a][1],f);return f;function c(r,t,e){var o,i,f,u,a=r.indexOf(".");return a>-1?(o=r.substr(0,a),i=[r.substr(a+1),t],f=n.order([i]),function(r,n){var t=r[o],i=n[o],u=t&&"object"==typeof t,a=i&&"object"==typeof i;if(u||a){u||(t={}),a||(i={});var c=f(t,i);if(c)return c}return e?e(r,n):0}):function(n,o){var i=n[r],f=o[r];return i>f?t:i<f?-t:i!==u&&f===u?t:i===u&&f!==u?-t:e?e(n,o):0}}}return v=1,h.order=function(n,t){var e,o=arguments.length;if(1===o){if((e=r.call(this,n))instanceof Error)throw e;return e}if(2===o){if(n instanceof Array){if((e=r.call(this,t))instanceof Error)throw e;return n.sort(e)}throw new Error("Invalid argument type: "+n)}throw new Error("Invalid arguments length: "+o)},h}().order,t.update=t.prototype.update=function(){if(l)return p;function r(n){var t,e=this;if("function"==typeof n)return n;if(n=n||{},!Object.keys(n))return null;if("object"!=typeof n)return new Error("Invalid update operator type: "+r);var o=[];return Object.keys(n).forEach((function(r){if(!t){var i=e.$update[r];if(i){var f=n[r],u={},a={};for(var c in f){if(/(^|\.)__proto__(\.|$)/.test(c))return void(t=new Error("Invalid target: "+c));var s=f[c],v=c.indexOf(".");if(v>-1){var h=c.substr(0,v),l=c.substr(v+1);a[h]=a[h]||{},a[h][l]=s}else u[c]=s}if(Object.keys(u).length){var p=i(u);o.push(p)}Object.keys(a).length&&Object.keys(a).forEach((function(n){var t={};t[r]=a[n];var i=e.update(t);o.push((function(r){var t=r[n];return"object"!=typeof t&&(t=r[n]={}),r[n]=i(t),r}))}))}else t=new Error("Unknown update operator: "+r)}})),t||(o.length<2?o.shift()||null:function(r){var n=r.length;return function(t){for(var e=0;e<n;e++)t=(0,r[e])(t);return t}}(o))}return l=1,p.update=function(n,t){var e,o=arguments.length;if(1===o){if((e=r.call(this,n))instanceof Error)throw e;return e}if(2===o){if(n instanceof Array){if((e=r.call(this,t))instanceof Error)throw e;return n.map(e)}throw new Error("Invalid argument type: "+n)}throw new Error("Invalid arguments length: "+o)},p}().update,t.system=t.prototype.system=d,t.$where=t.prototype.$where=new r,t.$update=t.prototype.$update=new n,y=t}();return r(b)}();