@idraw/util 0.2.0-y.0 → 0.3.0-alpha.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.
@@ -0,0 +1 @@
1
+ export * as default from './index';
package/esm/default.js ADDED
@@ -0,0 +1,2 @@
1
+ import * as default_1 from './index';
2
+ export default default_1;
package/esm/esm.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './index';
2
+ export * as default from './index';
package/esm/esm.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './index';
2
+ import * as default_1 from './index';
3
+ export default default_1;
package/esm/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { delay, compose, throttle } from './lib/time';
2
+ import { downloadImageFromCanvas } from './lib/file';
3
+ import { toColorHexStr, toColorHexNum, isColorStr } from './lib/color';
4
+ import { createUUID } from './lib/uuid';
5
+ import { deepClone } from './lib/data';
6
+ import istype from './lib/istype';
7
+ import { loadImage, loadSVG, loadHTML } from './lib/loader';
8
+ import Context from './lib/context';
9
+ import is from './lib/is';
10
+ import check from './lib/check';
11
+ export { is, check, delay, compose, throttle, loadImage, loadSVG, loadHTML, downloadImageFromCanvas, toColorHexStr, toColorHexNum, isColorStr, createUUID, istype, deepClone, Context, };
package/esm/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import { delay, compose, throttle } from './lib/time';
2
+ import { downloadImageFromCanvas } from './lib/file';
3
+ import { toColorHexStr, toColorHexNum, isColorStr } from './lib/color';
4
+ import { createUUID } from './lib/uuid';
5
+ import { deepClone } from './lib/data';
6
+ import istype from './lib/istype';
7
+ import { loadImage, loadSVG, loadHTML } from './lib/loader';
8
+ import Context from './lib/context';
9
+ import is from './lib/is';
10
+ import check from './lib/check';
11
+ export { is, check, delay, compose, throttle, loadImage, loadSVG, loadHTML, downloadImageFromCanvas, toColorHexStr, toColorHexNum, isColorStr, createUUID, istype, deepClone, Context, };
package/package.json CHANGED
@@ -1,16 +1,19 @@
1
1
  {
2
2
  "name": "@idraw/util",
3
- "version": "0.2.0-y.0",
3
+ "version": "0.3.0-alpha.2",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs.js",
6
- "module": "dist/index.es.js",
6
+ "module": "dist/index.esm.js",
7
7
  "unpkg": "dist/index.global.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "scripts": {
10
10
  "test": "echo \"Error: no test specified\" && exit 1"
11
11
  },
12
12
  "files": [
13
- "dist"
13
+ "dist/*.ts",
14
+ "dist/*.js",
15
+ "esm/*.js",
16
+ "esm/*.ts"
14
17
  ],
15
18
  "repository": {
16
19
  "type": "git",
@@ -22,8 +25,11 @@
22
25
  "homepage": "https://github.com/idrawjs/idraw#readme",
23
26
  "author": "chenshenhai",
24
27
  "license": "MIT",
28
+ "devDependencies": {
29
+ "@idraw/types": "^0.3.0-alpha.2"
30
+ },
25
31
  "publishConfig": {
26
32
  "access": "public"
27
33
  },
28
- "gitHead": "0d2b64c879118697515c8b78783a0577a36d0104"
34
+ "gitHead": "36f2aa4ef4e834ef329b3e4385b21b849e7f460f"
29
35
  }
package/dist/index.es.js DELETED
@@ -1,227 +0,0 @@
1
- function compose(middleware) {
2
- return function (context, next) {
3
- return dispatch(0);
4
- function dispatch(i) {
5
- var fn = middleware[i];
6
- if (i === middleware.length && next) {
7
- fn = next;
8
- }
9
- if (!fn)
10
- return Promise.resolve();
11
- try {
12
- return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
13
- }
14
- catch (err) {
15
- return Promise.reject(err);
16
- }
17
- }
18
- };
19
- }
20
- function delay(time) {
21
- return new Promise(function (resolve) {
22
- setTimeout(function () {
23
- resolve();
24
- }, time);
25
- });
26
- }
27
- function throttle(fn, timeout) {
28
- var timer = -1;
29
- return function () {
30
- var args = [];
31
- for (var _i = 0; _i < arguments.length; _i++) {
32
- args[_i] = arguments[_i];
33
- }
34
- if (timer > 0) {
35
- return;
36
- }
37
- timer = setTimeout(function () {
38
- fn.apply(void 0, args);
39
- timer = -1;
40
- }, timeout);
41
- };
42
- }
43
-
44
- function downloadImageFromCanvas(canvas, opts) {
45
- var filename = opts.filename, _a = opts.type, type = _a === void 0 ? 'image/jpeg' : _a;
46
- var stream = canvas.toDataURL(type);
47
- var downloadLink = document.createElement('a');
48
- downloadLink.href = stream;
49
- downloadLink.download = filename;
50
- var downloadClickEvent = document.createEvent('MouseEvents');
51
- downloadClickEvent.initEvent('click', true, false);
52
- downloadLink.dispatchEvent(downloadClickEvent);
53
- }
54
-
55
- function toColorHexNum(color) {
56
- return parseInt(color.replace(/^\#/, '0x'));
57
- }
58
- function toColorHexStr(color) {
59
- return '#' + color.toString(16);
60
- }
61
- function isColorStr(color) {
62
- return typeof color === 'string' && /^\#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(color);
63
- }
64
-
65
- function createUUID() {
66
- function str4() {
67
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
68
- }
69
- return "" + str4() + str4() + "-" + str4() + "-" + str4() + "-" + str4() + "-" + str4() + str4() + str4();
70
- }
71
-
72
- function deepClone(target) {
73
- function _clone(t) {
74
- var type = is(t);
75
- if (['Null', 'Number', 'String', 'Boolean', 'Undefined'].indexOf(type) >= 0) {
76
- return t;
77
- }
78
- else if (type === 'Array') {
79
- var arr_1 = [];
80
- t.forEach(function (item) {
81
- arr_1.push(_clone(item));
82
- });
83
- return arr_1;
84
- }
85
- else if (type === 'Object') {
86
- var obj_1 = {};
87
- var keys = Object.keys(t);
88
- keys.forEach(function (key) {
89
- obj_1[key] = _clone(t[key]);
90
- });
91
- return obj_1;
92
- }
93
- }
94
- return _clone(target);
95
- }
96
- function is(data) {
97
- return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1];
98
- }
99
-
100
- function parsePrototype(data) {
101
- var typeStr = Object.prototype.toString.call(data) || '';
102
- var result = typeStr.replace(/(\[object|\])/ig, '').trim();
103
- return result;
104
- }
105
- var istype = {
106
- type: function (data, lowerCase) {
107
- var result = parsePrototype(data);
108
- return lowerCase === true ? result.toLocaleLowerCase() : result;
109
- },
110
- array: function (data) {
111
- return parsePrototype(data) === 'Array';
112
- },
113
- json: function (data) {
114
- return parsePrototype(data) === 'Object';
115
- },
116
- function: function (data) {
117
- return parsePrototype(data) === 'Function';
118
- },
119
- asyncFunction: function (data) {
120
- return parsePrototype(data) === 'AsyncFunction';
121
- },
122
- string: function (data) {
123
- return parsePrototype(data) === 'String';
124
- },
125
- number: function (data) {
126
- return parsePrototype(data) === 'Number';
127
- },
128
- undefined: function (data) {
129
- return parsePrototype(data) === 'Undefined';
130
- },
131
- null: function (data) {
132
- return parsePrototype(data) === 'Null';
133
- },
134
- promise: function (data) {
135
- return parsePrototype(data) === 'Promise';
136
- },
137
- nodeList: function (data) {
138
- return parsePrototype(data) === 'NodeList';
139
- },
140
- imageData: function (data) {
141
- return parsePrototype(data) === 'ImageData';
142
- }
143
- };
144
-
145
- var Image = window.Image, Blob = window.Blob, FileReader = window.FileReader;
146
- function loadImage(src) {
147
- return new Promise(function (resolve, reject) {
148
- var img = new Image;
149
- img.onload = function () {
150
- resolve(img);
151
- };
152
- img.onabort = reject;
153
- img.onerror = reject;
154
- img.src = src;
155
- });
156
- }
157
- function loadSVG(svg) {
158
- return new Promise(function (resolve, reject) {
159
- var _svg = svg;
160
- var image = new Image();
161
- var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
162
- var reader = new FileReader();
163
- reader.readAsDataURL(blob);
164
- reader.onload = function (event) {
165
- var _a;
166
- var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
167
- image.onload = function () {
168
- resolve(image);
169
- };
170
- image.src = base64;
171
- };
172
- reader.onerror = function (err) {
173
- reject(err);
174
- };
175
- });
176
- }
177
- function loadHTML(html, opts) {
178
- var width = opts.width, height = opts.height;
179
- return new Promise(function (resolve, reject) {
180
- var _svg = "\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + (width || '') + "\" height = \"" + (height || '') + "\">\n <foreignObject width=\"100%\" height=\"100%\">\n <div xmlns = \"http://www.w3.org/1999/xhtml\">\n " + html + "\n </div>\n </foreignObject>\n </svg>\n ";
181
- var image = new Image();
182
- var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
183
- var reader = new FileReader();
184
- reader.readAsDataURL(blob);
185
- reader.onload = function (event) {
186
- var _a;
187
- var base64 = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.result;
188
- image.onload = function () {
189
- resolve(image);
190
- };
191
- image.src = base64;
192
- };
193
- reader.onerror = function (err) {
194
- reject(err);
195
- };
196
- });
197
- }
198
-
199
- var index = {
200
- time: {
201
- delay: delay,
202
- compose: compose,
203
- throttle: throttle,
204
- },
205
- loader: {
206
- loadImage: loadImage,
207
- loadSVG: loadSVG,
208
- loadHTML: loadHTML,
209
- },
210
- file: {
211
- downloadImageFromCanvas: downloadImageFromCanvas,
212
- },
213
- color: {
214
- toColorHexStr: toColorHexStr,
215
- toColorHexNum: toColorHexNum,
216
- isColorStr: isColorStr,
217
- },
218
- uuid: {
219
- createUUID: createUUID
220
- },
221
- istype: istype,
222
- data: {
223
- deepClone: deepClone,
224
- }
225
- };
226
-
227
- export { index as default };