@koeroesi86/canvas-grid 1.0.15 → 26.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +27 -0
  2. package/build/types/cell-background/index.d.ts +25 -0
  3. package/build/types/cell-value/index.d.ts +21 -0
  4. package/build/types/character/index.d.ts +21 -0
  5. package/build/types/header/index.d.ts +25 -0
  6. package/build/types/rectangle/index.d.ts +12 -0
  7. package/build/types/text/index.d.ts +31 -0
  8. package/build/types/types.d.ts +87 -0
  9. package/build/types/utils/canvas.d.ts +1 -0
  10. package/build/types/utils/collection.d.ts +18 -0
  11. package/build/types/utils/column-store.d.ts +16 -0
  12. package/build/types/utils/data-store.d.ts +49 -0
  13. package/build/types/utils/jsx.d.ts +40 -0
  14. package/build/types/utils/layer.d.ts +26 -0
  15. package/build/types/utils/loading.d.ts +17 -0
  16. package/build/types/utils/lru-cache.d.ts +11 -0
  17. package/build/types/utils/renderer-registry.d.ts +13 -0
  18. package/build/types/utils/renderer.d.ts +26 -0
  19. package/build/types/utils/row-store.d.ts +37 -0
  20. package/build/types/utils/selectors.d.ts +3 -0
  21. package/build/types/web-component.d.ts +189 -0
  22. package/build/web-component.js +1 -732
  23. package/build/web-component.js.map +1 -1
  24. package/build/web-component.mjs +1226 -0
  25. package/build/web-component.mjs.map +1 -0
  26. package/package.json +32 -32
  27. package/.babelrc +0 -14
  28. package/.eslintrc +0 -7
  29. package/.github/CODEOWNERS +0 -1
  30. package/.github/SECURITY.md +0 -9
  31. package/.github/dependabot.yml +0 -11
  32. package/.github/workflows/ci.yml +0 -32
  33. package/.github/workflows/codeql.yml +0 -26
  34. package/.github/workflows/publish.yml +0 -51
  35. package/components/cellBackground/index.jsx +0 -86
  36. package/components/cellValue/index.jsx +0 -80
  37. package/components/character/index.jsx +0 -82
  38. package/components/component.scss +0 -45
  39. package/components/example.js +0 -56
  40. package/components/header/index.jsx +0 -100
  41. package/components/rectangle/index.jsx +0 -65
  42. package/components/text/index.jsx +0 -131
  43. package/components/utils/collection.js +0 -58
  44. package/components/utils/drawingLayer.js +0 -66
  45. package/components/utils/jsx.js +0 -57
  46. package/components/utils/loading.js +0 -58
  47. package/components/utils/renderer.js +0 -45
  48. package/components/utils/selectors.js +0 -13
  49. package/components/web-component.js +0 -582
  50. package/config/htmlGenerator.js +0 -36
  51. package/nodemon.json +0 -5
  52. package/public/index.html +0 -29
  53. package/scripts/build.js +0 -30
  54. package/scripts/dev.js +0 -64
  55. package/server/example.js +0 -59
  56. package/server/exampleServe.js +0 -3
  57. package/webpack.config.js +0 -151
@@ -1,86 +0,0 @@
1
- import React from '../utils/jsx';
2
- import Renderer from "../utils/renderer";
3
- import RectangleRenderer from "../rectangle";
4
-
5
- class CellBackground extends Renderer {
6
- constructor(options) {
7
- super(options);
8
- this.isScrolling = false;
9
-
10
- this.rectangleRenderer = new RectangleRenderer({
11
- // canvas: this.options.canvas,
12
- alpha: this.options.alpha,
13
- });
14
- this.ctx = this.options.canvas.getContext('2d', { alpha: this.options.alpha });
15
-
16
- this._hasBottomBorder = this.options.borderBottomWidth > 0 && this.options.borderBottomColor;
17
- this._hasRightBorder = this.options.borderRightWidth > 0 && this.options.borderRightColor;
18
-
19
- this.render = this.render.bind(this);
20
- }
21
-
22
- render(value = '', x = 0, y = 0, width = 0, height = 0, isUpdated = false, isValueUpdated = false, forcedDraw = false, diff = 0) {
23
- this.rectangleRenderer.color = this.options.background;
24
- this.ctx.drawImage(<this.rectangleRenderer
25
- key={`cellBackground-${this.rectangleRenderer.color}-${width}-${height}`}
26
- width={width}
27
- height={height}
28
- />, x, y);
29
-
30
- if (this.options.enableFlashing && !this.isScrolling) {
31
- let alpha = 0;
32
- const totalFlashDuration = this.options.flashInDuration + this.options.flashOutDuration;
33
- if (diff <= totalFlashDuration) {
34
- if (diff < this.options.flashInDuration) {
35
- alpha = (diff / this.options.flashInDuration);
36
- } else if (diff > this.options.flashInDuration && diff <= totalFlashDuration) {
37
- alpha = ((totalFlashDuration - diff) / this.options.flashOutDuration);
38
- }
39
-
40
- if (alpha > 0) {
41
- this.rectangleRenderer.color = `rgba(${this.options.flashRGB}, ${alpha.toFixed(3)})`;
42
- this.ctx.drawImage(<this.rectangleRenderer
43
- key={`cellBackground-${this.rectangleRenderer.color}-${width}-${height}`}
44
- width={width}
45
- height={height}
46
- />, x, y);
47
- }
48
- }
49
- }
50
-
51
- if (this._hasRightBorder) {
52
- this.rectangleRenderer.color = this.options.borderRightColor;
53
- this.ctx.drawImage(<this.rectangleRenderer
54
- key={`cellBorder-${this.rectangleRenderer.color}-${this.options.borderRightWidth}-${height}`}
55
- width={this.options.borderRightWidth}
56
- height={height}
57
- />, x + width - this.options.borderRightWidth, y);
58
- }
59
-
60
- if (this._hasBottomBorder) {
61
- this.rectangleRenderer.color = this.options.borderBottomColor;
62
- this.ctx.drawImage(<this.rectangleRenderer
63
- key={`cellBorder-${this.rectangleRenderer.color}-${width}-${this.options.borderBottomWidth}`}
64
- width={width}
65
- height={this.options.borderBottomWidth}
66
- />, x, y + height - this.options.borderBottomWidth);
67
- }
68
- }
69
- }
70
-
71
- CellBackground.defaultOptions = {
72
- /** @param {HTMLCanvasElement} */
73
- canvas: null,
74
- enableFlashing: true,
75
- background: '#333',
76
- alpha: true,
77
- flashInDuration: 200,
78
- flashOutDuration: 400,
79
- flashRGB: '214, 144, 13',
80
- borderRightWidth: 0,
81
- borderRightColor: '#fff',
82
- borderBottomWidth: 0,
83
- borderBottomColor: '#fff',
84
- };
85
-
86
- export default CellBackground;
@@ -1,80 +0,0 @@
1
- import React from "../utils/jsx";
2
- import Renderer from "../utils/renderer";
3
- import TextRenderer from "../text";
4
-
5
- export default class CellValueRenderer extends Renderer {
6
- constructor(options) {
7
- super(options);
8
-
9
- this.renderer = new TextRenderer({
10
- font: this.options.font,
11
- color: this.options.color,
12
- characters: this.options.characters,
13
- });
14
-
15
- this.ctx = this.options.canvas.getContext('2d', { alpha: this.options.alpha });
16
-
17
- this._isScrolling = false;
18
- this.clearCache = this.clearCache.bind(this);
19
- this.preWarmCache = this.preWarmCache.bind(this);
20
- this.render = this.render.bind(this);
21
- }
22
-
23
- set font(font) {
24
- this.options.font = font;
25
- this.renderer.font = font;
26
- }
27
-
28
- get font() {
29
- return this.options.font;
30
- }
31
-
32
- set color(color) {
33
- this.options.color = color;
34
- this.renderer.color = color;
35
- }
36
-
37
- get color() {
38
- return this.options.color;
39
- }
40
-
41
- set isScrolling(isScrolling) {
42
- this._isScrolling = isScrolling;
43
- }
44
-
45
- get isScrolling() {
46
- return this._isScrolling;
47
- }
48
-
49
- clearCache() {
50
- this.renderer.clearCache();
51
- React.invalidateCache((_, key) => /^cellValue-/.test(key))
52
- }
53
-
54
- preWarmCache() {
55
- this.renderer.preWarmCache();
56
- }
57
-
58
- render(value, x, y, width, height, isUpdated, isValueUpdated, forcedDraw, diff) {
59
- if (isValueUpdated || forcedDraw) {
60
- this.ctx.clearRect(x, y, width, height);
61
- this.ctx.drawImage(<this.renderer
62
- value={value}
63
- x={0}
64
- y={0}
65
- width={width}
66
- height={height}
67
- isUpdated={isUpdated}
68
- isValueUpdated={isValueUpdated}
69
- forcedDraw={forcedDraw}
70
- diff={diff}
71
- />, x, y);
72
- }
73
- }
74
- }
75
-
76
- CellValueRenderer.defaultOptions = {
77
- font: '12px monospace',
78
- color: '#fff',
79
- characters: ''
80
- };
@@ -1,82 +0,0 @@
1
- import Collection from "../utils/collection";
2
- import Renderer from "../utils/renderer";
3
-
4
- export default class CharacterRenderer extends Renderer {
5
- constructor(options = {}) {
6
- super(options);
7
-
8
- this.charWidthCache = new Collection();
9
-
10
- this.canvas = document.createElement('canvas');
11
- this.ctx = this.canvas.getContext('2d', { alpha: this.options.alpha });
12
- this.ctx.textAlign = 'left';
13
- this.ctx.textBaseline = 'top';
14
-
15
- this.render = this.render.bind(this);
16
- this.clearCache = this.clearCache.bind(this);
17
- this.getCharacterWidth = this.getCharacterWidth.bind(this);
18
- }
19
-
20
- set font(font) {
21
- if (this.options.font !== font) {
22
- this.options.font = font;
23
- }
24
- }
25
-
26
- clearCache() {
27
- this.charWidthCache.clear();
28
- }
29
-
30
- getCharacterWidth(character = '') {
31
- const cached = this.charWidthCache.get(character);
32
- if (cached) {
33
- return cached;
34
- }
35
- if (this.ctx.font !== this.options.font) {
36
- this.ctx.font = this.options.font;
37
- this.ctx.textAlign = 'left';
38
- this.ctx.textBaseline = 'top';
39
- }
40
- const width = Math.ceil(this.ctx.measureText(character).width);
41
- this.charWidthCache.set(character, width);
42
- return width;
43
- }
44
-
45
- render(props = {}) {
46
- const {
47
- value = '',
48
- x = 0,
49
- y = 0,
50
- width = 0,
51
- height = 0,
52
- isUpdated = false,
53
- isValueUpdated = false,
54
- forcedDraw = false,
55
- diff = 0
56
- } = props;
57
- this.canvas.width = this.getCharacterWidth(value);
58
- this.canvas.height = height;
59
-
60
- this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
61
-
62
- if (this.ctx.font !== this.options.font) {
63
- this.ctx.font = this.options.font;
64
- this.ctx.textAlign = 'left';
65
- this.ctx.textBaseline = 'top';
66
- }
67
- if (this.ctx.fillStyle !== this.options.color) {
68
- this.ctx.fillStyle = this.options.color;
69
- }
70
-
71
- this.ctx.fillText(value, 0, 0);
72
-
73
- return this.canvas;
74
- }
75
- }
76
-
77
- CharacterRenderer.defaultOptions = {
78
- font: '12px monospace',
79
- color: '#fff',
80
- alpha: true,
81
- clearBeforeRender: true,
82
- };
@@ -1,45 +0,0 @@
1
- canvas-grid {
2
- position: relative;
3
- display: block;
4
-
5
- &, & * {
6
- box-sizing: border-box;
7
- margin: 0;
8
- padding: 0;
9
- }
10
-
11
- .drawingLayer {
12
- position: absolute;
13
- top: 0;
14
- left: 0;
15
- width: 100%;
16
- height: 100%;
17
-
18
- &.background {
19
- //z-index: 1;
20
- //background-color: #333;
21
- }
22
- &.content {
23
- //z-index: 2;
24
- }
25
- &.header {
26
- //z-index: 3;
27
- }
28
- }
29
-
30
- .scrollArea {
31
- position: absolute;
32
- top: 0;
33
- left: 0;
34
- width: 100%;
35
- height: 100%;
36
- overflow: auto;
37
- z-index: 10;
38
- backface-visibility: hidden;
39
- will-change: transform;
40
-
41
- &> div {
42
- will-change: scroll-position, transform;
43
- }
44
- }
45
- }
@@ -1,56 +0,0 @@
1
- import "./web-component";
2
-
3
- let canvasGrid;
4
-
5
- const init = () => {
6
-
7
- canvasGrid = document.querySelector('canvas-grid');
8
- canvasGrid.font = '12px "Roboto", sans-serif';
9
- canvasGrid.headerFont = '13px "Roboto", sans-serif';
10
-
11
- const ws = new WebSocket(`ws://localhost:${process.env.DEV_PORT || 3000}`);
12
- // ws.addEventListener('open', e => {
13
- // console.log('open', e);
14
- // });
15
- ws.addEventListener('message', e => {
16
- try {
17
- const data = JSON.parse(e.data);
18
- if (data.columns && Array.isArray(data.columns)) {
19
- // const headers = data.columns.map((key, index) => ({
20
- // id: key,
21
- // index: index,
22
- // displayName: `${key}`,
23
- // }));
24
- canvasGrid.setColumns(data.columns);
25
- }
26
- if (data.all) {
27
- canvasGrid.setValues(data.all);
28
- }
29
- if (data.row) {
30
- canvasGrid.queueUpdate(data.row)
31
- }
32
- } catch (e) {
33
- console.error(e)
34
- }
35
- });
36
- ws.addEventListener('close', e => {
37
- console.log('Connection closed, reconnecting...');
38
- setTimeout(init, 1000)
39
- });
40
- ws.addEventListener('error', e => {
41
- console.log('error', e);
42
- });
43
- };
44
-
45
- window.addEventListener('load', () => {
46
- init();
47
-
48
- if (module.hot) {
49
- if (!window.webpackHotUpdate) {
50
- window.webpackHotUpdate = () => {
51
- window.location.reload();
52
- };
53
- }
54
- module.hot.accept(); //['./example.js', './index.js']
55
- }
56
- });
@@ -1,100 +0,0 @@
1
- import React from "../utils/jsx";
2
- import RectangleRenderer from "../rectangle";
3
- import TextRenderer from "../text";
4
- import Renderer from "../utils/renderer";
5
-
6
- class HeaderRenderer extends Renderer {
7
- constructor(options) {
8
- super(options);
9
-
10
- this.rectangleRenderer = new RectangleRenderer({
11
-
12
- });
13
-
14
- this.textRenderer = new TextRenderer({
15
- canvas: this.options.canvas,
16
- font: this.options.font,
17
- color: this.options.color,
18
- characters: this.options.characters,
19
- });
20
- this.ctx = this.options.canvas.getContext('2d', { alpha: true });
21
-
22
- this._hasBottomBorder = !!(this.options.borderBottomWidth > 0 && this.options.borderBottomColor);
23
- this._hasRightBorder = !!(this.options.borderRightWidth > 0 && this.options.borderRightColor);
24
-
25
- this.render = this.render.bind(this);
26
- }
27
-
28
- set font(font) {
29
- this.textRenderer.font = font;
30
- }
31
-
32
- get font() {
33
- return this.textRenderer.font;
34
- }
35
-
36
- render(value = '', x = 0, y = 0, width = 0, height = 0, isUpdated = false, isValueUpdated = false, forcedDraw = false, diff = 0) {
37
- if (width === 0 || height === 0) return this.canvas;
38
- this.rectangleRenderer.color = this.options.background;
39
- this.ctx.drawImage(<this.rectangleRenderer
40
- key={`headerBackground-${this.rectangleRenderer.color}-${width}-${height}`}
41
- width={width}
42
- height={height}
43
- />, x, y);
44
-
45
- if (this._hasRightBorder) {
46
- this.rectangleRenderer.color = this.options.borderRightColor;
47
- this.ctx.drawImage(
48
- <this.rectangleRenderer
49
- key={`headerBorder-${this.rectangleRenderer.color}-${this.options.borderRightWidth}-${height}`}
50
- height={height}
51
- width={this.options.borderRightWidth}
52
- />,
53
- x + width - this.options.borderRightWidth,
54
- y
55
- );
56
- }
57
-
58
- if (this._hasBottomBorder) {
59
- this.rectangleRenderer.color = this.options.borderBottomColor;
60
- this.ctx.drawImage(
61
- <this.rectangleRenderer
62
- key={`headerBorder-${this.rectangleRenderer.color}-${width}-${this.options.borderBottomWidth}`}
63
- width={width}
64
- height={this.options.borderBottomWidth}
65
- />,
66
- x,
67
- y + height - this.options.borderBottomWidth
68
- );
69
- }
70
-
71
- this.textRenderer.color = this.options.color;
72
- this.ctx.drawImage(<this.textRenderer
73
- key={`headerText-${value}-${this.textRenderer.font}`}
74
- value={value}
75
- x={0}
76
- y={0}
77
- width={width}
78
- height={height}
79
- isUpdated={isUpdated}
80
- isValueUpdated={isValueUpdated}
81
- forcedDraw={forcedDraw}
82
- diff={diff}
83
- />, x, y);
84
- }
85
- }
86
-
87
- HeaderRenderer.defaultOptions = {
88
- /** @param {HTMLCanvasElement} */
89
- canvas: null,
90
- background: '#333',
91
- font: '13px monospace',
92
- color: '#fff',
93
- characters: '',
94
- borderRightWidth: 1,
95
- borderRightColor: '#fff',
96
- borderBottomWidth: 1,
97
- borderBottomColor: '#fff',
98
- };
99
-
100
- export default HeaderRenderer;
@@ -1,65 +0,0 @@
1
- import Renderer from "../utils/renderer";
2
-
3
- class RectangleRenderer extends Renderer {
4
- constructor(options) {
5
- super(options);
6
-
7
- this.canvas = document.createElement('canvas');
8
- this.ctx = this.canvas.getContext('2d', { alpha: this.options.alpha });
9
-
10
- this.drawBackground = this.drawBackground.bind(this);
11
- this.clearBackground = this.clearBackground.bind(this);
12
- this.render = this.render.bind(this);
13
- }
14
-
15
- set color(color) {
16
- this.options.color = color;
17
- }
18
-
19
- get color() {
20
- return this.options.color;
21
- }
22
-
23
- clearBackground(x = 0, y = 0, width = 0, height = 0) {
24
- this.ctx.clearRect(x, y, width, height);
25
- }
26
-
27
- drawBackground(x = 0, y = 0, width = 0, height = 0) {
28
- if (this.ctx.fillStyle !== this.options.color) {
29
- this.ctx.fillStyle = this.options.color;
30
- }
31
- this.ctx.fillRect(x, y, width, height);
32
- }
33
-
34
- render(p = {}) {
35
- const props = {
36
- ...RectangleRenderer.defaultProps,
37
- ...p
38
- };
39
- this.canvas.width = props.width;
40
- this.canvas.height = props.height;
41
- this.clearBackground(props.x, props.y, props.width, props.height);
42
- if (this.options.color) this.drawBackground(props.x, props.y, props.width, props.height);
43
-
44
- return this.canvas;
45
- }
46
- }
47
-
48
- RectangleRenderer.defaultProps = {
49
- value: '',
50
- x: 0,
51
- y: 0,
52
- width: 0,
53
- height: 0,
54
- isUpdated: false,
55
- isValueUpdated: false,
56
- forcedDraw: false,
57
- diff: 0
58
- };
59
-
60
- RectangleRenderer.defaultOptions = {
61
- alpha: true,
62
- color: '#000'
63
- };
64
-
65
- export default RectangleRenderer;
@@ -1,131 +0,0 @@
1
- import React from '../utils/jsx';
2
- import Renderer from "../utils/renderer";
3
- import CharacterRenderer from "../character";
4
-
5
- class TextRenderer extends Renderer {
6
- constructor(options = {}) {
7
- super(options);
8
-
9
- this.canvas = document.createElement('canvas');
10
- this.ctx = this.canvas.getContext('2d', { alpha: this.options.alpha });
11
- this.ctx.textAlign = 'left';
12
- this.ctx.textBaseline = 'top';
13
-
14
- this.characterRenderer = new CharacterRenderer({
15
- font: this.options.font,
16
- color: this.options.color,
17
- });
18
-
19
- this.render = this.render.bind(this);
20
- this.clearCache = this.clearCache.bind(this);
21
- this.preWarmCache = this.preWarmCache.bind(this);
22
- this.getTextWidth = this.getTextWidth.bind(this);
23
- }
24
-
25
- set font(font) {
26
- if (this.options.font !== font) {
27
- this.options.font = font;
28
- this.characterRenderer.font = font;
29
- this.clearCache();
30
- }
31
- }
32
-
33
- get font() {
34
- return this.options.font;
35
- }
36
-
37
- clearCache() {
38
- this.characterRenderer.clearCache();
39
- React.invalidateCache((_, key) => /^characterRenderer-/.test(key))
40
- }
41
-
42
- preWarmCache() {
43
- // const { characters } = this.options;
44
- // for (let i = 0; i < characters.length; i++) {
45
- // const character = characters.charAt(i);
46
- // const buffer = <this.characterRenderer
47
- // key={`characterRenderer-${character}-${this.options.font}`}
48
- // value={character}
49
- // width={this.getTextWidth(character)}
50
- // height={50}
51
- // />;
52
- // }
53
- }
54
-
55
- getTextWidth(text) {
56
- let totalWidth = 0;
57
- for (let i = 0; i < text.length; i++) {
58
- const character = text.charAt(i);
59
- totalWidth += this.characterRenderer.getCharacterWidth(character);
60
- }
61
- return totalWidth;
62
- }
63
-
64
- render(props = {}) {
65
- const {
66
- value = '',
67
- x = 0,
68
- y = 0,
69
- width = 0,
70
- height = 0,
71
- isUpdated = false,
72
- isValueUpdated = false,
73
- forcedDraw = false,
74
- diff = 0
75
- } = props;
76
- this.ctx.clearRect(x, y, width, height);
77
-
78
- const ellipsisWidth = this.characterRenderer.getCharacterWidth(this.options.ellipsis);
79
- const verticalPadding = this.options.cellPadding.left + this.options.cellPadding.right;
80
- const availableWidth = width - ellipsisWidth - verticalPadding;
81
- let currentXOffset = this.options.cellPadding.left;
82
- const yOffset = y + this.options.cellPadding.top;
83
- const maxHeight = height - this.options.cellPadding.top - this.options.cellPadding.bottom;
84
- let currentWidth = 0;
85
-
86
- // break down to character for better caching
87
- for (let i = 0; i < value.length; i++) {
88
- const character = value.charAt(i);
89
- const characterWidth = Math.round(this.characterRenderer.getCharacterWidth(character));
90
-
91
- if (Math.ceil(currentWidth + characterWidth) < availableWidth) {
92
- this.ctx.drawImage(<this.characterRenderer
93
- key={`characterRenderer-${character}-${this.options.font}`}
94
- value={character}
95
- width={characterWidth}
96
- height={maxHeight}
97
- />, x + currentXOffset, yOffset);
98
- currentWidth += characterWidth;
99
- currentXOffset += characterWidth;
100
- } else {
101
- this.ctx.drawImage(<this.characterRenderer
102
- key={`characterRenderer-${this.options.ellipsis}-${this.options.font}`}
103
- value={this.options.ellipsis}
104
- width={characterWidth}
105
- height={maxHeight}
106
- />, x + currentXOffset, yOffset);
107
- break;
108
- }
109
- }
110
-
111
- return this.canvas;
112
- }
113
- }
114
-
115
- TextRenderer.defaultOptions = {
116
- ellipsis: '…',
117
- characters: '',
118
- alpha: true,
119
- cellPadding: {
120
- top: 3,
121
- right: 3,
122
- bottom: 3,
123
- left: 3,
124
- },
125
- font: '12px monospace',
126
- color: '#fff',
127
- isEllipsisRight: true,
128
- canvas: null,
129
- };
130
-
131
- export default TextRenderer;
@@ -1,58 +0,0 @@
1
- class Collection {
2
- constructor() {
3
- this.items = {};
4
- this.keys = [];
5
-
6
- this.get = this.get.bind(this);
7
- this.set = this.set.bind(this);
8
- this.clear = this.clear.bind(this);
9
- this.forEach = this.forEach.bind(this);
10
- }
11
-
12
- get(key) {
13
- return this.items[key];
14
- }
15
-
16
- set(key, value) {
17
- this.items[key] = value;
18
- if (!this.keys.includes(key)) {
19
- this.keys.push(key);
20
- }
21
- }
22
-
23
- add(value) {
24
- this.set(this.keys.length, value);
25
- }
26
-
27
- delete(key) {
28
- this.items[key] = null;
29
- delete this.items[key];
30
- const index = this.keys.indexOf(key);
31
- if (index !== -1) {
32
- this.keys.splice(index, 1);
33
- }
34
- }
35
-
36
- clear(matcher) {
37
- if (typeof matcher === 'function') {
38
- this.forEach((item, key) => {
39
- if (matcher(item, key)) this.delete(key);
40
- });
41
- } else {
42
- this.items = {};
43
- this.keys.splice(0);
44
- }
45
- }
46
-
47
- forEach(fn) {
48
- this.keys.forEach(key => {
49
- fn(this.items[key], key);
50
- });
51
- }
52
-
53
- get length() {
54
- return this.keys.length;
55
- }
56
- }
57
-
58
- export default Collection;