Skip to content

Commit

Permalink
Add linting rules, add synchronous and asynchronous checks and approp…
Browse files Browse the repository at this point in the history
…riate

tests
  • Loading branch information
colbygk committed Jan 17, 2018
1 parent 3744b54 commit 0552483
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/*
static/*
build/*
intl/*
locales/*
**/*.min.js
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: ['scratch', 'scratch/node','scratch/es6'],
};

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# scratch-asset-types

A library for detecting types for Scratch backend services that is optimized for the specific file types that Scratch services depend on.

### Thanks to file-type

This library is derived from the more general [file-type](https://www.npmjs.com/package/file-type) npm module.
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const typesList = require('./lib/typeslist');
const readChunk = require('read-chunk');

module.exports = input => {
module.exports.bufferCheck = input => {
const buf = (input instanceof Uint8Array) ? input : new Uint8Array(input);

if (!(buf && buf.length > 1)) {
Expand Down Expand Up @@ -80,14 +80,13 @@ module.exports = input => {
return null;
};

module.exports.async = (fileName) => {
return new Promise(function(resolve, reject) {
const chunkPromise = readChunk(fileName, 0, 128);
chunkPromise.then(function(result) {
resolve(module.exports(result));
}, function(err) {
reject(err);
});
module.exports.asyncCheck = fileName => new Promise((resolve, reject) => {
const chunkPromise = readChunk(fileName, 0, 128);
chunkPromise.then(result => {
resolve(module.exports.bufferCheck(result));
}, err => {
reject(err);
});
};
});

module.exports.syncCheck = fileName => module.exports.bufferCheck(readChunk.sync(fileName, 0, 128));
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"babel-eslint": "^8.0.3",
"eslint": "^4.13.1",
"eslint-config-scratch": "^5.0.0",
"read-chunk": "^2.1.0",
"tap": "^11.0.0"
},
"dependencies": {
"read-chunk": "^2.1.0"
}
}
13 changes: 9 additions & 4 deletions test/unit/filetype.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
const tap = require('tap');
const readChunk = require('read-chunk');
const fileType = require('../../index');
const typesList = require('../../lib/typeslist');

const checkList = [
'gif', 'jpg', 'json', 'png', 'webp', 'zip'];

tap.test('check-types', t => {

checkList.forEach(thisType => {
const buffer = readChunk.sync(`./test/fixtures/test.${thisType}`, 0, 128);
const detectedType = fileType(buffer);
const detectedType = fileType.syncCheck(`./test/fixtures/test.${thisType}`);
t.ok(detectedType);
t.equals(detectedType.ext, typesList[thisType].ext);
t.equals(detectedType.mime, typesList[thisType].mime);
});

t.end();
});

checkList.forEach(thisType => fileType.asyncCheck(`./test/fixtures/test.${thisType}`)
.then(result => tap.test('check async results', t => {
t.ok(result);
t.equals(result.ext, typesList[thisType].ext);
t.equals(result.mime, typesList[thisType].mime);
t.end();
})));

0 comments on commit 0552483

Please sign in to comment.