Skip to content

Commit 5359fd9

Browse files
committed
ts conversion, init
1 parent 544a008 commit 5359fd9

33 files changed

Lines changed: 5525 additions & 8948 deletions

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.github

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 4,
4+
"semi": false,
5+
"singleQuote": true
6+
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- 14
3+
- 14

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Your mosh mode doesn't have to do anything specific -- if you think it's cool, w
1212

1313
Most of Datamosh's current modes overwrite existing Red/Green/Blue (RGB) pixel values with something random. If you get stuck or just need some inspiration, check out these examples.
1414

15-
- [Vana mode](https://github.com/mster/datamosh/blob/master/lib/modes/vana.js)
16-
- [Schifty mode](https://github.com/mster/datamosh/blob/master/lib/modes/schifty.js)
17-
- [Before and after images](https://github.com/mster/datamosh#example-images)
15+
- [Vana mode](https://github.com/mster/datamosh/blob/master/lib/modes/vana.js)
16+
- [Schifty mode](https://github.com/mster/datamosh/blob/master/lib/modes/schifty.js)
17+
- [Before and after images](https://github.com/mster/datamosh#example-images)
1818

1919
Once you have a stub function and some cool name for it, you'll want to import it into Datamosh.
2020

@@ -28,14 +28,14 @@ To import your mode, you have two options:
2828
1. Fork datamosh and run `npm install` within the directory.
2929
2. On your fork, create a new branch named `username/work-description`, where `username` is your GitHub username and `work-description` is a short description for your contribution.
3030

31-
For example: `mster/new-mode`
31+
For example: `mster/new-mode`
3232

3333
3. Commit your work to the branch you created.
3434
4. When you're ready for review or to submit your contribution, double check a few things.
3535

36-
- ✓ First, make sure your fork is up to date. If your fork is out of date, you will need to rebase.
36+
- ✓ First, make sure your fork is up to date. If your fork is out of date, you will need to rebase.
3737

38-
- ✓ Next, assure that your code pases `npm test`.
38+
- ✓ Next, assure that your code pases `npm test`.
3939

4040
5. If your contribution is more than a single commit, squash all commits into one.
4141
6. Open a pull request to `mster:master`.
@@ -57,7 +57,7 @@ To test if your code passes, run the test command:
5757

5858
As of v1.0.0, tests consist of:
5959

60-
- linting using Prettier
60+
- linting using Prettier
6161

6262
## Need Help
6363

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ $ npm install datamosh
1313
# Usage
1414

1515
```js
16-
const mosh = require("datamosh");
16+
const mosh = require('datamosh')
1717

18-
let imgBuff = await readFile("/full/path/to/image.png");
18+
let imgBuff = await readFile('/full/path/to/image.png')
1919

20-
let moshedBuff = await mosh(imgBuff, "vaporwave");
20+
let moshedBuff = await mosh(imgBuff, 'vaporwave')
2121
```
2222

2323
Reading/Writing the moshed image
2424

2525
```js
26-
mosh("~/image.png", null, "~/moshed_image.png");
26+
mosh('~/image.png', null, '~/moshed_image.png')
2727

2828
// because mode is null, a random mode will be chosen
2929
```
@@ -32,16 +32,16 @@ Moshing a buffer with callbacks
3232

3333
```js
3434
const cb = (err, data) => {
35-
if (!err) writeFile("/path/to/out.gif", data);
36-
};
35+
if (!err) writeFile('/path/to/out.gif', data)
36+
}
3737

38-
mosh(imgBuff, "vana", cb);
38+
mosh(imgBuff, 'vana', cb)
3939
```
4040

4141
Using multiple modes on a single image, applied with respect to order.
4242

4343
```js
44-
let moshedBuff = await mosh(imgBuff, ["fatcat", "vaporwave", "walter"]);
44+
let moshedBuff = await mosh(imgBuff, ['fatcat', 'vaporwave', 'walter'])
4545

4646
// ['vana', null, null] is also valid => ['vana', random, random]
4747
```
@@ -52,9 +52,9 @@ let moshedBuff = await mosh(imgBuff, ["fatcat", "vaporwave", "walter"]);
5252

5353
Takes input `source` Buffer/Path, returns an encoded Buffer with the applied modes.
5454

55-
- `mode`, the mosh mode to apply to the source image. Multiple modes may be passed using an array of modes. Any `null` values are replaced with a random mode.
56-
- `cb (err, data)`, when using callbacks.
57-
- `writePath`, the path to write the moshed image to.
55+
- `mode`, the mosh mode to apply to the source image. Multiple modes may be passed using an array of modes. Any `null` values are replaced with a random mode.
56+
- `cb (err, data)`, when using callbacks.
57+
- `writePath`, the path to write the moshed image to.
5858

5959
Paths may use the tilde (~) character. Datamosh validates read and write paths, replacing tilde with the path to the home directory.
6060

@@ -69,15 +69,15 @@ Datamosh allows you to set custom moshing modes. As of `v1.1.0`, this may be aco
6969
For mosh function starter code, see the included template file located [here](https://github.com/mster/datamosh/blob/master/lib/modes/template).
7070

7171
```js
72-
const datamosh = require("datamosh");
72+
const datamosh = require('datamosh')
7373

7474
function newMode(data, width, height) {
75-
// your cool code goes here!
75+
// your cool code goes here!
7676

77-
return data;
77+
return data
7878
}
7979

80-
datamosh.MODES.newMode = newMode;
80+
datamosh.MODES.newMode = newMode
8181
```
8282

8383
## Example Images
@@ -96,4 +96,4 @@ Vaporwave was created by user [@tlaskey](https://github.com/tlaskey)
9696

9797
Check out this list of awesome apps that use `datamosh`!
9898

99-
- [JanMichaelBot](https://github.com/tlaskey/JanMichaelBot) by user [@Tlaskey](https://github.com/tlaskey)
99+
- [JanMichaelBot](https://github.com/tlaskey/JanMichaelBot) by user [@Tlaskey](https://github.com/tlaskey)

example.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { readFile } from 'fs/promises'
2+
import datamosh from './lib/datamosh';
3+
4+
const sampleImagePath: string = './tests/fixtures/rgb.png';
5+
6+
(async () => {
7+
const src = await readFile(sampleImagePath)
8+
const result = await datamosh({ src })
9+
})();

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require("./lib/mosh");
1+
module.exports = require('./lib/mosh')

jest.config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"bail": 1,
3+
"verbose": true,
4+
"preset": "ts-jest",
5+
"testMatch": [
6+
"**/tests/**/*.test.ts"
7+
]
8+
}

lib/datamosh.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __generator = (this && this.__generator) || function (thisArg, body) {
12+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14+
function verb(n) { return function (v) { return step([n, v]); }; }
15+
function step(op) {
16+
if (f) throw new TypeError("Generator is already executing.");
17+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
18+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19+
if (y = 0, t) op = [op[0] & 2, t.value];
20+
switch (op[0]) {
21+
case 0: case 1: t = op; break;
22+
case 4: _.label++; return { value: op[1], done: false };
23+
case 5: _.label++; y = op[1]; op = [0]; continue;
24+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
25+
default:
26+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30+
if (t[2]) _.ops.pop();
31+
_.trys.pop(); continue;
32+
}
33+
op = body.call(thisArg, _);
34+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36+
}
37+
};
38+
Object.defineProperty(exports, "__esModule", { value: true });
39+
var encode = require("image-encode");
40+
var decode = require("image-decode");
41+
var file_type_1 = require("file-type");
42+
var modes_1 = require("./modes");
43+
function datamosh(_a) {
44+
var src = _a.src, _b = _a.mode, mode = _b === void 0 ? 'random' : _b, modes = _a.modes;
45+
return __awaiter(this, void 0, void 0, function () {
46+
var fileTypeMeta, decodedSourceImg, moshedImg, encodedMoshedImg;
47+
return __generator(this, function (_c) {
48+
switch (_c.label) {
49+
case 0: return [4 /*yield*/, (0, file_type_1.fromBuffer)(src)];
50+
case 1:
51+
fileTypeMeta = _c.sent();
52+
if (!fileTypeMeta ||
53+
!['jpg', 'jpeg', 'png', 'gif', 'tif', 'bmp'].includes(fileTypeMeta === null || fileTypeMeta === void 0 ? void 0 : fileTypeMeta.ext))
54+
throwError('Invalid source buffer.');
55+
decodedSourceImg = decode(src);
56+
moshedImg = Buffer.from(decodedSourceImg.data);
57+
if (mode && modes_1.default[mode])
58+
moshedImg = modes_1.default[mode](moshedImg);
59+
if (modes &&
60+
modes.filter(function (m) { return modes_1.default.includes(m); }).length === modes.length) {
61+
modes.forEach(function (mode) { return moshedImg = modes_1.default[mode](moshedImg); });
62+
}
63+
encodedMoshedImg = encode(moshedImg, [decodedSourceImg.width, decodedSourceImg.height], fileTypeMeta.ext);
64+
return [2 /*return*/, encodedMoshedImg];
65+
}
66+
});
67+
});
68+
}
69+
function throwError(msg) {
70+
var error = new Error(msg);
71+
throw error;
72+
}
73+
exports.default = datamosh;

lib/datamosh.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict'
2+
3+
import * as encode from 'image-encode'
4+
import * as decode from 'image-decode'
5+
import { fromBuffer } from 'file-type'
6+
7+
import supportedModes from './modes'
8+
9+
type MoshArguments = {
10+
src: Buffer
11+
mode?: string
12+
modes?: Array<string>
13+
}
14+
15+
async function datamosh({ src, mode = 'random', modes }: MoshArguments) {
16+
// validate file-type
17+
const fileTypeMeta: any = await fromBuffer(src)
18+
if (
19+
!fileTypeMeta ||
20+
!['jpg', 'jpeg', 'png', 'gif', 'tif', 'bmp'].includes(fileTypeMeta?.ext)
21+
)
22+
throwError('Invalid source buffer.')
23+
24+
// decode image
25+
const decodedSourceImg: any = decode(src)
26+
27+
// apply mode(s)
28+
let moshedImg = Buffer.from(decodedSourceImg.data)
29+
if (mode && supportedModes[mode])
30+
moshedImg = supportedModes[mode](moshedImg)
31+
if (
32+
modes &&
33+
modes.filter((m) => supportedModes.includes(m)).length === modes.length
34+
) {
35+
modes.forEach(
36+
(mode) => moshedImg = supportedModes[mode](moshedImg)
37+
)
38+
}
39+
40+
// encode image
41+
const encodedMoshedImg = encode(
42+
moshedImg,
43+
[decodedSourceImg.width, decodedSourceImg.height],
44+
fileTypeMeta.ext
45+
)
46+
return encodedMoshedImg
47+
}
48+
49+
function throwError(msg: string) {
50+
const error: Error = new Error(msg)
51+
throw error
52+
}
53+
54+
export default datamosh

0 commit comments

Comments
 (0)