@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 3.07 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import t from"./define-nonenumerable-read-only-property.js";import e from"./define-nonenumerable-read-only-accessor.js";import r from"./../symbol/iterator.js";import n from"./define-property.js";var i=n;function s(t){return i(this,"next",{configurable:!1,enumerable:!0,get:function(){return this._next}}),this.value=t,i(this,"_next",{configurable:!1,enumerable:!1,writable:!0,value:null}),i(this,"_prev",{configurable:!1,enumerable:!1,writable:!0,value:null}),this}var h=t,l=e,o=r,_=s;function u(){return this instanceof u?(this._length=0,this._first=null,this._last=null,this):new u}h(u.prototype,"clear",(function(){return this._length=0,this._first=null,this._last=null,this})),h(u.prototype,"first",(function(){if(this._length)return this._first})),h(u.prototype,"insert",(function(t,e){var r;if(t===this._last)return this.push(e);for(r=this._first;r!==this._last&&r!==t;)r=r._next;if(r===this._last)throw new Error("invalid argument. The list does not contain the provided list node.");return r=new _(e),t._next._prev=r,r._next=t._next,t._next=r,r._prev=t,this._length+=1,this})),h(u.prototype,"iterator",(function(){var t,e,r,n,i;return r=this,i=-1,t=this.toArray(),h(e={},"next",(function(){if(i+=1,n||i>=t.length)return{done:!0};return{value:t[i],done:!1}})),h(e,"return",(function(t){if(n=!0,arguments.length)return{value:t,done:!0};return{done:!0}})),o&&h(e,o,(function(){return r.iterator()})),e})),h(u.prototype,"last",(function(){if(this._length)return this._last})),l(u.prototype,"length",(function(){return this._length})),h(u.prototype,"pop",(function(){var t;return this._length&&(t=this._last.value,this._last._prev?(this._last=this._last._prev,this._last._next=null):(this._first=null,this._last=null),this._length-=1),t})),h(u.prototype,"push",(function(t){var e;return e=new _(t),0===this._length?(this._first=e,this._last=e):(e._prev=this._last,this._last._next=e,this._last=e),this._length+=1,this})),h(u.prototype,"remove",(function(t){var e,r;if(t===this._first)return this.shift();if(t===this._last)return this.pop();for(e=t.value,r=this._first;r!==this._last&&r!==t;)r=r._next;if(r===this._last)throw new Error("invalid argument. The list does not contain the provided list node.");return t._prev._next=t._next,t._next._prev=t._prev,this._length-=1,e})),h(u.prototype,"shift",(function(){var t;return this._length&&(t=this._first.value,this._first._next?(this._first=this._first._next,this._first._prev=null):(this._first=null,this._last=null),this._length-=1),t})),h(u.prototype,"toArray",(function(){var t,e,r;for(e=[],t=this._first,r=0;r<this._length;r++)e.push(t.value),t=t.next;return e})),h(u.prototype,"toJSON",(function(){var t={type:"linked-list"};return t.data=this.toArray(),t})),h(u.prototype,"unshift",(function(t){var e;return e=new _(t),0===this._length?(this._first=e,this._last=e):(e._next=this._first,this._first._prev=e,this._first=e),this._length+=1,this}));var a=u;export default a;
//# sourceMappingURL=linked-list.js.map