From 0eab2630ad1de5043c75ec870b207b3698ff716c Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 9 Nov 2024 15:35:05 -0500 Subject: [PATCH] added fileText method --- package.json | 2 +- src/files.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0d4c7bd..5d85bfa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.22.3", + "version": "0.22.4", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/files.ts b/src/files.ts index af5adff..3713be5 100644 --- a/src/files.ts +++ b/src/files.ts @@ -52,6 +52,21 @@ export function fileBrowser(options: {accept?: string, multiple?: boolean} = {}) }); } +/** + * Extract text from a file + * + * @param file File to extract text from + * @return {Promise} File contents + */ +export function fileText(file: any): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => resolve(reader.result); + reader.onerror = () => reject(reader.error); + reader.readAsText(file); + }); +} + /** * Create timestamp intended for filenames from a date *