Skip to content

Commit 5783a19

Browse files
author
Andrea Giammarchi
committed
Refactoring out most common dependencies
1 parent f9e767a commit 5783a19

File tree

7 files changed

+88
-59
lines changed

7 files changed

+88
-59
lines changed

cjs/utils.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
'use strict';
2+
const Map = (require('@ungap/essential-map'));
3+
24
const append = (get, parent, children, start, end, before) => {
35
if ((end - start) < 2)
46
parent.insertBefore(get(children[start], 1), before);
@@ -98,22 +100,6 @@ const INSERTION = 1;
98100
const SKIP = 0;
99101
const SKIP_OND = 50;
100102

101-
/* istanbul ignore next */
102-
const Rel = typeof Map === 'undefined' ?
103-
function () {
104-
const k = [], v = [];
105-
return {
106-
has: key => -1 < k.indexOf(key),
107-
get: key => v[k.indexOf(key)],
108-
set: (key, value) => {
109-
const i = k.indexOf(key);
110-
v[i < 0 ? (k.push(key) - 1) : i] = value;
111-
}
112-
};
113-
} :
114-
Map
115-
;
116-
117103
const HS = (
118104
futureNodes,
119105
futureStart,
@@ -135,7 +121,7 @@ const HS = (
135121
for (let i = 1; i < minLen; i++)
136122
tresh[i] = currentEnd;
137123

138-
const keymap = new Rel;
124+
const keymap = new Map;
139125
for (let i = currentStart; i < currentEnd; i++)
140126
keymap.set(currentNodes[i], i);
141127

@@ -282,7 +268,7 @@ const applyDiff = (
282268
currentLength,
283269
before
284270
) => {
285-
const live = new Rel;
271+
const live = new Map;
286272
const length = diff.length;
287273
let currentIndex = currentStart;
288274
let i = 0;

esm/utils.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Map from '@ungap/essential-map';
2+
13
export const append = (get, parent, children, start, end, before) => {
24
if ((end - start) < 2)
35
parent.insertBefore(get(children[start], 1), before);
@@ -90,22 +92,6 @@ const INSERTION = 1;
9092
const SKIP = 0;
9193
const SKIP_OND = 50;
9294

93-
/* istanbul ignore next */
94-
const Rel = typeof Map === 'undefined' ?
95-
function () {
96-
const k = [], v = [];
97-
return {
98-
has: key => -1 < k.indexOf(key),
99-
get: key => v[k.indexOf(key)],
100-
set: (key, value) => {
101-
const i = k.indexOf(key);
102-
v[i < 0 ? (k.push(key) - 1) : i] = value;
103-
}
104-
};
105-
} :
106-
Map
107-
;
108-
10995
const HS = (
11096
futureNodes,
11197
futureStart,
@@ -127,7 +113,7 @@ const HS = (
127113
for (let i = 1; i < minLen; i++)
128114
tresh[i] = currentEnd;
129115

130-
const keymap = new Rel;
116+
const keymap = new Map;
131117
for (let i = currentStart; i < currentEnd; i++)
132118
keymap.set(currentNodes[i], i);
133119

@@ -274,7 +260,7 @@ const applyDiff = (
274260
currentLength,
275261
before
276262
) => {
277-
const live = new Rel;
263+
const live = new Map;
278264
const length = diff.length;
279265
let currentIndex = currentStart;
280266
let i = 0;

index.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
var domdiff = (function () {
22
'use strict';
33

4+
/*! (c) Andrea Giammarchi - ISC */
5+
var window = null || /* istanbul ignore next */{};
6+
try {
7+
window.Map = Map;
8+
} catch (Map) {
9+
window.Map = function Map() {
10+
var i = 0;
11+
var k = [];
12+
var v = [];
13+
return {
14+
delete: function _delete(key) {
15+
var had = contains(key);
16+
if (had) {
17+
k.splice(i, 1);
18+
v.splice(i, 1);
19+
}
20+
return had;
21+
},
22+
get: function get(key) {
23+
return contains(key) ? v[i] : void 0;
24+
},
25+
has: function has(key) {
26+
return contains(key);
27+
},
28+
set: function set(key, value) {
29+
v[contains(key) ? i : k.push(key) - 1] = value;
30+
return this;
31+
}
32+
};
33+
function contains(v) {
34+
i = k.indexOf(v);
35+
return -1 < i;
36+
}
37+
};
38+
}
39+
var Map$1 = window.Map;
40+
441
var append = function append(get, parent, children, start, end, before) {
542
if (end - start < 2) parent.insertBefore(get(children[start], 1), before);else {
643
var fragment = parent.ownerDocument.createDocumentFragment();
@@ -65,24 +102,6 @@ var INSERTION = 1;
65102
var SKIP = 0;
66103
var SKIP_OND = 50;
67104

68-
/* istanbul ignore next */
69-
var Rel = typeof Map === 'undefined' ? function () {
70-
var k = [],
71-
v = [];
72-
return {
73-
has: function has(key) {
74-
return -1 < k.indexOf(key);
75-
},
76-
get: function get(key) {
77-
return v[k.indexOf(key)];
78-
},
79-
set: function set(key, value) {
80-
var i = k.indexOf(key);
81-
v[i < 0 ? k.push(key) - 1 : i] = value;
82-
}
83-
};
84-
} : Map;
85-
86105
var HS = function HS(futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges) {
87106

88107
var k = 0;
@@ -94,7 +113,7 @@ var HS = function HS(futureNodes, futureStart, futureEnd, futureChanges, current
94113

95114
for (var i = 1; i < minLen; i++) {
96115
tresh[i] = currentEnd;
97-
}var keymap = new Rel();
116+
}var keymap = new Map$1();
98117
for (var _i = currentStart; _i < currentEnd; _i++) {
99118
keymap.set(currentNodes[_i], _i);
100119
}for (var _i2 = futureStart; _i2 < futureEnd; _i2++) {
@@ -215,7 +234,7 @@ var OND = function OND(futureNodes, futureStart, rows, currentNodes, currentStar
215234
};
216235

217236
var applyDiff = function applyDiff(diff, get, parentNode, futureNodes, futureStart, currentNodes, currentStart, currentLength, before) {
218-
var live = new Rel();
237+
var live = new Map$1();
219238
var length = diff.length;
220239
var currentIndex = currentStart;
221240
var i = 0;

min.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"build": "npm run cjs && npm run bundle && npm run min && npm run test && npm run size",
1010
"bundle": "rollup --config rollup.config.js && npm run cleanup",
1111
"cleanup": "echo \"$(cat index.js | sed 's/return exports;/return domdiff;/' | sed -e 's/exports.*;//g' | sed 's/exports//' | sed 's/}({}));/}());/')\" > index.js",
12-
"cjs": "ascjs ./esm ./cjs",
12+
"cjs": "ascjs ./esm ./cjs && sed -i.bck 's/(m => m.__esModule ? m.default : m)//' cjs/utils.js && rm cjs/utils.js.bck",
1313
"coveralls": "cat ./coverage/lcov.info | coveralls",
1414
"min": "uglifyjs index.js --support-ie8 --comments=/^!/ -cmo min.js",
1515
"size": "echo \"gzip: $(cat min.js | gzip -c9 | wc -c)\" && echo \"brotli: $(cat min.js | brotli | wc -c)\" && echo ''",
@@ -41,13 +41,16 @@
4141
"istanbul": "^0.4.5",
4242
"rollup": "^0.52.0",
4343
"rollup-plugin-babel": "^3.0.7",
44+
"rollup-plugin-node-resolve": "^3.4.0",
4445
"tressa": "^0.3.1",
4546
"uglify-js": "^2.8.29"
4647
},
4748
"directories": {
4849
"test": "test"
4950
},
50-
"dependencies": {},
51+
"dependencies": {
52+
"@ungap/essential-map": "^0.1.5"
53+
},
5154
"repository": {
5255
"type": "git",
5356
"url": "git+https://github.com/WebReflection/domdiff.git"

rollup.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import babel from 'rollup-plugin-babel';
2+
import resolve from 'rollup-plugin-node-resolve';
23

34
export default {
45
input: 'esm/index.js',
56
plugins: [
7+
resolve({
8+
module: true
9+
}),
610
babel({
711
plugins: require('./babel-plugins.json')
812
})
913
],
14+
context: 'null',
15+
moduleContext: 'null',
1016
output: {
1117
exports: 'named',
1218
file: 'index.js',

0 commit comments

Comments
 (0)