Skip to content

Commit

Permalink
update package
Browse files Browse the repository at this point in the history
  • Loading branch information
henperi committed Apr 5, 2021
1 parent 5787a39 commit 4c052d5
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
.nyc_output
coverage
dist
tsconfig.json
.nycrc.json
package-lock.json
.eslintrc
src
61 changes: 53 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

## This package helps to validate images on the DOM.

A small and simple utility package that can validate any kind of image,
A small and simple utility package that can validate any kind of image,

- It can check and detect corrupt image files.
- it can check and detect files whose extensions have been renamed to look like images.
- It can check and detect files whose extensions have been renamed to look like images.
- It can check and detect remote or local image urls

It supports actual image files(from input), or any string image url (could be a data url, local url or a remote url on a server).

## Examples
## Installation

```
npm install image-validator
```

## Examples & Usage

### ES6

```typescript
import { validateImage } from "../src/index";
import { validateImage } from "image-validator";

// To validate a file
const fileValidation = async (file: File) => {
Expand All @@ -29,9 +39,44 @@ const urlValidation = async (url: string) => {

// We can ensure a throw for bad images by passing a second argument
const urlValidationThatThrowsOnError = async (url: string) => {
const isValidImage = await validateImage(url, { throw: true });
console.log(isValidImage);
// expected output ==> true (for valid images)
// expected output ==> Error (for invalid images)
try {
const isValidImage = await validateImage(url, { throw: true });
console.log(isValidImage);
// expected output ==> true (for valid images)
} catch (error) {
console.log(error);
// expected output ==> "The media resource is either invalid, corrupt or unsuitable" (for invalid images)
}
};
```

### ES5

```javascript
const validateImage = require("./index").validateImage;

// To validate a file
const fileValidation = (file: File) => {
validateImage(file).then((validationResult) => console.log(validationResult));
// expected output ==> true or false
};

// To validate a url, can be a remote url on a server or a local url on system
const urlValidation = async (url: string) => {
validateImage(url).then((validationResult) => console.log(validationResult));
// expected output ==> true or false
};

// We can ensure a throw for bad images by passing a second argument
const urlValidationThatThrowsOnError = async (url: string) => {
validateImage(url, { throw: true })
.then((validationResult) => {
console.log(validationResult);
// expected output ==> true (for valid images)
})
.catch((error) => {
console.log(isValidImage);
// expected output ==> "The media resource is either invalid, corrupt or unsuitable" (for invalid images)
});
};
```
File renamed without changes.
2 changes: 1 addition & 1 deletion dist/index.js → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const validateImage = (src, config) => {
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")
? reject("The media resource is either invalid, corrupt or unsuitable")
: resolve(false));
image.addEventListener("load", () => resolve(true), false);
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const validateImage = (
return new Promise((resolve, reject) => {
image.addEventListener("error", () =>
config?.throw
? reject("The selected media resource is unsuitable")
? reject("The media resource is either invalid, corrupt or unsuitable")
: resolve(false)
);

Expand Down
7 changes: 3 additions & 4 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { validateImage } from "../src/index";
import { expect } from "chai";

describe("validateImage unit tests", () => {
it("Should return false when an invalid image url is supplied", async () => {});
it("Should return false when an invalid image url is supplied", async () => {
//
});
});
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "commonjs",
"strict": true,
"declaration": true,
"outDir": "./dist",
"outDir": "./"
},
"exclude": ["tests", "dist", "node_modules"]
"exclude": ["tests", "dist", "node_modules", "index.d.ts"]
}

0 comments on commit 4c052d5

Please sign in to comment.