@xassist/xassist-object
Version:
general helper functions for JavaScript objects
7 lines • 2.51 kB
JavaScript
/**
* @preserve
* https://github.com/GregBee2/xassist-object#readme Version 1.0.5.
* Copyright 2018 Gregory Beirens.
* Created on Tue, 17 Apr 2018 10:38:57 GMT.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@xassist/xassist-eventdispatcher")):"function"==typeof define&&define.amd?define(["exports","@xassist/xassist-eventdispatcher"],e):e(t.xa=t.xa||{},t.xa)}(this,function(t,c){"use strict";function e(t){this.object=t,c.EventDispatcher.call(this,this),this.currentValues={}}((e.prototype=Object.create(c.EventDispatcher.prototype)).constructor=e).prototype.onChange=function(o,t,e){var r=this,n=!1;if(!o||!this.object.hasOwnProperty(o))throw new ReferenceError("key does not exist in Object");this.hasEvent("changeKey"+o)||(this.registerEvent("changeKey"+o,this.object),this.currentValues[o]=this.object[o],n=!0),c.EventDispatcher.prototype.on.call(this,"changeKey"+o,t,e),n&&Object.defineProperty(this.object,o,{set:function(t){var e=r.currentValues[o];(r.currentValues[o]=t)!==e&&r.fire("changeKey"+o,t,e,o,r.object)},get:function(){return r.currentValues[o]}})},e.prototype.assign=function(){if("function"==typeof Object.assign)return Object.assign.apply(null,[this.object].concat(Array.prototype.slice.call(arguments)));if(null==this.object)throw new TypeError("Cannot convert undefined or null to object");for(var t=Object(this.object),e=0;e<arguments.length;e++){var o=arguments[e];if(null!=o)for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&(t[r]=o[r])}return t},e.prototype.clone=function(){return JSON.parse(JSON.stringify(this.object))},e.prototype.mergeUnique=function(n){var c=this;return null!=n&&Object.keys(this.object).forEach(function(t){var e,o,r;n.hasOwnProperty(t)&&(c.object[t]=(r=c.object[t],e=typeof r,o=n[t],"boolean"===e?!!o:"number"===e?Number(o):"string"===e?String(o):o))}),this.object},e.prototype.toArray=function(){var e=this;return Object.keys(this.object).map(function(t){return e.object[t]})},e.prototype.toMapArray=function(){var e=this;return Object.keys(this.object).map(function(t){return[t,e.object[t]]})},e.prototype.forEach=function(e,o){var r=this;return o||(o=this.object),Object.keys(this.object).forEach(function(t){e.call(o,r.object[t],t,r.object)},o)},e.prototype.map=function(e,o){var r=this;o||(o=this.object);var n={};return Object.keys(this.object).forEach(function(t){n[t]=e.call(o,r.object[t],t,r.object)},o),n},t.object=function(t){return new e(t)},Object.defineProperty(t,"__esModule",{value:!0})});