Skip to content

Commit

Permalink
feat: add tool to analyse random data
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbruijn committed May 17, 2020
1 parent 2a33420 commit 25cd19f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*
!.gitignore
!random-data-analyser
Empty file.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"tools:hsc": "npm run hex-string-converter",
"map-gnu-symbol": "node_modules/.bin/ts-node src/map-gnu-symbol/cli",
"tools:mgs": "npm run map-gnu-symbol",
"random-data-analyser": "node_modules/.bin/ts-node src/random-data-analyser/cli",
"tools:rda": "npm run random-data-analyser",
"vidavidorra-logo": "node_modules/.bin/ts-node src/vidavidorra-logo/cli",
"tools:vl": "npm run vidavidorra-logo"
},
Expand Down
27 changes: 27 additions & 0 deletions src/random-data-analyser/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as validator from '../helpers/validator';
import { Arguments, randomDataAnalyser, tool } from './random-data-analyser';
import inquirer from 'inquirer';
import path from 'path';

const questions = [
{
type: 'input',
name: 'dataPath',
message: [
'What is the path of the data to analyse?',
'Data MUST be a JSON array containing random bytes (0-255).',
].join('\n '),
validate: validator.existingFile,
default: './data/random.json',
},
{
type: 'input',
name: 'outputDirectory',
message: 'What directory do you want to save the analysed data in?',
default: path.join('./build', tool.name),
},
];

inquirer.prompt<Arguments>(questions).then((answers) => {
randomDataAnalyser(answers);
});
1 change: 1 addition & 0 deletions src/random-data-analyser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './random-data-analyser';
42 changes: 42 additions & 0 deletions src/random-data-analyser/pixel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export class Pixel {
constructor(
private red: number,
private green: number,
private blue: number,
private alpha = 1.0,
) {
this.validate();
}

set(red: number, green: number, blue: number, alpha = 1.0): void {
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;

this.validate();
}

RGB(): [number, number, number] {
return [this.red, this.green, this.blue];
}

RGBA(): [number, number, number, number] {
return [this.red, this.green, this.blue, this.alpha];
}

private validate(): void {
if (
this.red < 0 ||
this.red > 255 ||
this.green < 0 ||
this.green > 255 ||
this.blue < 0 ||
this.blue > 255 ||
this.alpha < 0.0 ||
this.alpha > 1.0
) {
throw new Error('Invalid colour value(s)!');
}
}
}
53 changes: 53 additions & 0 deletions src/random-data-analyser/random-data-analyser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Pixel } from './pixel';
import { Tool } from '../tool';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import sharp from 'sharp';

export const tool = new Tool(
'random-data-analyser',
'Analyse random data by creating an black-and-white pixel image with it.',
);

export interface Arguments {
dataPath: string;
outputDirectory: string;
}

export function randomDataAnalyser(args: Arguments): Promise<void> {
return new Promise((resolve) => {
try {
const randomData = JSON.parse(fs.readFileSync(args.dataPath, 'utf-8'));

const data: number[] = [];
const size = Math.floor(Math.sqrt(randomData.length));

randomData.forEach((e) => {
const pixel = new Pixel(0, 0, 0);
if (e % 2 === 0) {
pixel.set(255, 255, 255);
}

data.push(...pixel.RGB());
});

const image = sharp(Buffer.from(data), {
raw: { width: size, height: size, channels: 3 },
});

image
.toFile(path.join(args.outputDirectory, `random-${size}x${size}.png`))
.then(() => {
resolve();
})
.catch((e) => {
console.log(chalk.red(e));
process.exit(1);
});
} catch (e) {
console.log(chalk.red(e));
process.exit(1);
}
});
}
2 changes: 2 additions & 0 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Tool } from './tool';
import { tool as bufferConverter } from './buffer-converter';
import { tool as hexStringConverter } from './hex-string-converter';
import { tool as mapGnuSymbol } from './map-gnu-symbol';
import { tool as randomDataAnalyser } from './random-data-analyser';
import { tool as vidavidorraLogo } from './vidavidorra-logo';

class Tools {
Expand All @@ -12,6 +13,7 @@ class Tools {
bufferConverter,
hexStringConverter,
mapGnuSymbol,
randomDataAnalyser,
vidavidorraLogo,
];
}
Expand Down

0 comments on commit 25cd19f

Please sign in to comment.