@bbn/bbn 2.0.175 → 2.0.177

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,11 @@
1
+ /**
2
+ * Uploads a file in the browser and deal with its content.
3
+ *
4
+ * @method getFileContent
5
+ * @todo examples
6
+ * @global
7
+ * @memberof bbn.fn
8
+ *
9
+ * @returns {Promise}
10
+ */
11
+ export default function getFileContent(): void;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Uploads a file in the browser and deal with its content.
3
+ *
4
+ * @method getFileContent
5
+ * @todo examples
6
+ * @global
7
+ * @memberof bbn.fn
8
+ *
9
+ * @returns {Promise}
10
+ */
11
+ export default function getFileContent() {
12
+ let type = '';
13
+ let treatAs = 'text';
14
+ let success;
15
+ let failure;
16
+ let progress;
17
+ bbn.fn.each(arguments, (arg) => {
18
+ if (typeof arg === 'string') {
19
+ if (['binary', 'text'].includes(arg.toLowerCase())) {
20
+ treatAs = arg.toLowerCase();
21
+ }
22
+ else if (!type) {
23
+ type = arg;
24
+ if (type === 'image') {
25
+ type = "image/*";
26
+ }
27
+ }
28
+ }
29
+ else if (bbn.fn.isFunction(arg)) {
30
+ if (!success) {
31
+ success = arg;
32
+ }
33
+ else if (!failure) {
34
+ failure = arg;
35
+ }
36
+ else if (!progress) {
37
+ progress = arg;
38
+ }
39
+ }
40
+ });
41
+ let input = document.createElement("input");
42
+ input.type = "file";
43
+ if (type) {
44
+ input.setAttribute("accept", type);
45
+ }
46
+ input.onchange = function (event) {
47
+ var _a;
48
+ const file = (_a = input.files) === null || _a === void 0 ? void 0 : _a[0];
49
+ if (!file)
50
+ return;
51
+ const reader = new FileReader();
52
+ if (treatAs === 'binary') {
53
+ reader.readAsArrayBuffer(file);
54
+ }
55
+ else {
56
+ reader.readAsText(file, `UTF-8`);
57
+ }
58
+ reader.onload = function ({ target }) {
59
+ if (success) {
60
+ success(target === null || target === void 0 ? void 0 : target.result);
61
+ }
62
+ };
63
+ reader.onerror = function () {
64
+ if (failure) {
65
+ failure(`error reading file`);
66
+ }
67
+ };
68
+ };
69
+ input.click();
70
+ }
71
+ ;
package/dist/fn.d.ts CHANGED
@@ -102,6 +102,7 @@ import getDeviceType from './fn/browser/getDeviceType.js';
102
102
  import getEventData from './fn/browser/getEventData.js';
103
103
  import getField from './fn/object/getField.js';
104
104
  import getFieldValues from './fn/object/getFieldValues.js';
105
+ import getFileContent from './fn/ajax/getFileContent.js';
105
106
  import getIndex from './fn/html/getIndex.js';
106
107
  import getHtml from './fn/html/getHtml.js';
107
108
  import getHTMLOfSelection from './fn/html/getHTMLOfSelection.js';
@@ -350,6 +351,7 @@ declare const _default: {
350
351
  getEventData: typeof getEventData;
351
352
  getField: typeof getField;
352
353
  getFieldValues: typeof getFieldValues;
354
+ getFileContent: typeof getFileContent;
353
355
  getIndex: typeof getIndex;
354
356
  getHtml: typeof getHtml;
355
357
  getHTMLOfSelection: typeof getHTMLOfSelection;
package/dist/fn.js CHANGED
@@ -102,6 +102,7 @@ import getDeviceType from './fn/browser/getDeviceType.js';
102
102
  import getEventData from './fn/browser/getEventData.js';
103
103
  import getField from './fn/object/getField.js';
104
104
  import getFieldValues from './fn/object/getFieldValues.js';
105
+ import getFileContent from './fn/ajax/getFileContent.js';
105
106
  import getIndex from './fn/html/getIndex.js';
106
107
  import getHtml from './fn/html/getHtml.js';
107
108
  import getHTMLOfSelection from './fn/html/getHTMLOfSelection.js';
@@ -350,6 +351,7 @@ export default {
350
351
  getEventData,
351
352
  getField,
352
353
  getFieldValues,
354
+ getFileContent,
353
355
  getIndex,
354
356
  getHtml,
355
357
  getHTMLOfSelection,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.175",
3
+ "version": "2.0.177",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",