Skip to content

Commit

Permalink
implement image validator
Browse files Browse the repository at this point in the history
  • Loading branch information
henperi committed Apr 4, 2021
1 parent e12500e commit 4dbc65c
Show file tree
Hide file tree
Showing 12 changed files with 7,340 additions and 3 deletions.
26 changes: 26 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"env": {
"es6": true,
"node": true,
"mocha": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"no-console": "off",
"linebreak-style": "off",
"quotes": ["error", "double", { "allowTemplateLiterals": true }],
"keyword-spacing": ["error", { "before": true }],
"space-before-blocks": ["error"]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.nyc_output
coverage
11 changes: 11 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"cache": false,
"check-coverage": true,
"extension": [".ts"],
"include": ["**/src/*.ts"],
"reporter": ["cobertura", "text-summary"],
"statements": 90,
"branches": 90,
"functions": 90,
"lines": 90
}
Empty file added LICENSE
Empty file.
Empty file added README.md
Empty file.
9 changes: 9 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This method validates a File (input) or an image URL
* @param src - The image to validate, an input File or a local or remote image url
* @param config - an optional config object that defines the method behaviour
* @returns - Returns true for valid images, throws or returns false for invalid images
*/
export declare const validateImage: (src: string | File, config?: {
throw: boolean;
} | undefined) => Promise<boolean | undefined>;
31 changes: 31 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateImage = void 0;
/**
* This method validates a File (input) or an image URL
* @param src - The image to validate, an input File or a local or remote image url
* @param config - an optional config object that defines the method behaviour
* @returns - Returns true for valid images, throws or returns false for invalid images
*/
const validateImage = (src, config) => {
if (typeof window === "undefined") {
throw new Error("Cannot use this utility method in a non browser environment");
}
let url = "";
if (typeof src === "string") {
url = src;
}
else {
url = URL.createObjectURL(src);
}
const image = new Image();
image.src = url;
image.setAttribute("crossOrigin", "anonymous");
return new Promise((resolve, reject) => {
image.addEventListener("error", () => (config === null || config === void 0 ? void 0 : config.throw)
? reject("The selected media resource is unsuitable")
: resolve(false));
image.addEventListener("load", () => resolve(true), false);
});
};
exports.validateImage = validateImage;
Loading

0 comments on commit 4dbc65c

Please sign in to comment.