generated from vidavidorra/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tool to analyse random data
- Loading branch information
Showing
8 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* | ||
!.gitignore | ||
!random-data-analyser |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './random-data-analyser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)!'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters