Skip to content

Commit 5702b83

Browse files
timneutkensOlliV
authored andcommitted
Bring in deque for smaller bundle size (#27)
1 parent 2de48bd commit 5702b83

File tree

3 files changed

+139
-6
lines changed

3 files changed

+139
-6
lines changed

index.js

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,142 @@
22
const EventEmitter = require('events')
33
const util = require('util')
44

5-
// Packages
6-
const Deque = require('double-ended-queue')
5+
var isArray = Array.isArray;
6+
7+
// Deque is based on https://github.com/petkaantonov/deque/blob/master/js/deque.js
8+
// Released under the MIT License: https://github.com/petkaantonov/deque/blob/6ef4b6400ad3ba82853fdcc6531a38eb4f78c18c/LICENSE
9+
function Deque(capacity) {
10+
this._capacity = getCapacity(capacity);
11+
this._length = 0;
12+
this._front = 0;
13+
if (isArray(capacity)) {
14+
var len = capacity.length;
15+
for (var i = 0; i < len; ++i) {
16+
this[i] = capacity[i];
17+
}
18+
this._length = len;
19+
}
20+
}
21+
22+
Deque.prototype.push = function Deque$push(item) {
23+
var argsLength = arguments.length;
24+
var length = this._length;
25+
if (argsLength > 1) {
26+
var capacity = this._capacity;
27+
if (length + argsLength > capacity) {
28+
for (var i = 0; i < argsLength; ++i) {
29+
this._checkCapacity(length + 1);
30+
var j = (this._front + length) & (this._capacity - 1);
31+
this[j] = arguments[i];
32+
length++;
33+
this._length = length;
34+
}
35+
return length;
36+
}
37+
else {
38+
var j = this._front;
39+
for (var i = 0; i < argsLength; ++i) {
40+
this[(j + length) & (capacity - 1)] = arguments[i];
41+
j++;
42+
}
43+
this._length = length + argsLength;
44+
return length + argsLength;
45+
}
46+
47+
}
48+
49+
if (argsLength === 0) return length;
50+
51+
this._checkCapacity(length + 1);
52+
var i = (this._front + length) & (this._capacity - 1);
53+
this[i] = item;
54+
this._length = length + 1;
55+
return length + 1;
56+
};
57+
58+
Deque.prototype.pop = function Deque$pop() {
59+
var length = this._length;
60+
if (length === 0) {
61+
return void 0;
62+
}
63+
var i = (this._front + length - 1) & (this._capacity - 1);
64+
var ret = this[i];
65+
this[i] = void 0;
66+
this._length = length - 1;
67+
return ret;
68+
};
69+
70+
Deque.prototype.shift = function Deque$shift() {
71+
var length = this._length;
72+
if (length === 0) {
73+
return void 0;
74+
}
75+
var front = this._front;
76+
var ret = this[front];
77+
this[front] = void 0;
78+
this._front = (front + 1) & (this._capacity - 1);
79+
this._length = length - 1;
80+
return ret;
81+
};
82+
83+
Object.defineProperty(Deque.prototype, "length", {
84+
get: function() {
85+
return this._length;
86+
},
87+
set: function() {
88+
throw new RangeError("");
89+
}
90+
});
91+
92+
Deque.prototype._checkCapacity = function Deque$_checkCapacity(size) {
93+
if (this._capacity < size) {
94+
this._resizeTo(getCapacity(this._capacity * 1.5 + 16));
95+
}
96+
};
97+
98+
Deque.prototype._resizeTo = function Deque$_resizeTo(capacity) {
99+
var oldCapacity = this._capacity;
100+
this._capacity = capacity;
101+
var front = this._front;
102+
var length = this._length;
103+
if (front + length > oldCapacity) {
104+
var moveItemsCount = (front + length) & (oldCapacity - 1);
105+
arrayMove(this, 0, this, oldCapacity, moveItemsCount);
106+
}
107+
};
108+
109+
function arrayMove(src, srcIndex, dst, dstIndex, len) {
110+
for (var j = 0; j < len; ++j) {
111+
dst[j + dstIndex] = src[j + srcIndex];
112+
src[j + srcIndex] = void 0;
113+
}
114+
}
115+
116+
function pow2AtLeast(n) {
117+
n = n >>> 0;
118+
n = n - 1;
119+
n = n | (n >> 1);
120+
n = n | (n >> 2);
121+
n = n | (n >> 4);
122+
n = n | (n >> 8);
123+
n = n | (n >> 16);
124+
return n + 1;
125+
}
126+
127+
function getCapacity(capacity) {
128+
if (typeof capacity !== "number") {
129+
if (isArray(capacity)) {
130+
capacity = capacity.length;
131+
}
132+
else {
133+
return 16;
134+
}
135+
}
136+
return pow2AtLeast(
137+
Math.min(
138+
Math.max(16, capacity), 1073741824)
139+
);
140+
}
7141

8142
class ReleaseEmitter extends EventEmitter {}
9143

license.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
MIT License
22

33
Copyright (c) 2017, 2018 Zeit, Inc.
4+
Copyright (c) 2013 Petka Antonov (For the Deque library)
5+
46

57
Permission is hereby granted, free of charge, to any person obtaining a copy
68
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,5 @@
2424
"index.js",
2525
"rate-limit.js",
2626
"index.d.ts"
27-
],
28-
"dependencies": {
29-
"double-ended-queue": "2.1.0-0"
30-
}
27+
]
3128
}

0 commit comments

Comments
 (0)