Skip to content
Myr

Normalize scripts style

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrey Sitnik · committed by Andrey Sitnik · parent 16f15ed · Browse files · 3 files+117 added−115 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+117 added−115 removed

  • tests:+117 added−115 removed

3 files+117 added−115 removed

modified:test/alphabet+3 added-3 removed
1#!/usr/bin/node --experimental-worker1#!/usr/bin/node23const WORKERS = 845let spawnSync = require('child_process').spawnSync6let Worker = require('worker_threads').Worker5let { spawnSync } = require('child_process')6let { Worker } = require('worker_threads')7let chalk = require('chalk')8let path = require('path')9let fs = require('fs')10
73 unmodified lines
added:test/alphabet-genetic+114 added
1#!/usr/bin/node23let geneticAlgorithmConstructor = require('geneticalgorithm')4let { spawnSync } = require('child_process')5let chalk = require('chalk')6let path = require('path')7let fs = require('fs')8let gzipSize = require('gzip-size')910if (!process.argv[2]) {11 process.stderr.write(chalk.red('Pass a file with alphabet as argument\n'))12 process.exit(1)13}1415let input = path.join(process.cwd(), process.argv[2])16let output = path.join(__dirname, '..', 'compiled.js')1718spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])19let js = fs.readFileSync(output).toString()20fs.unlinkSync(output)2122let match = js.match(/[A-Za-z0-9-_]{30,}/)23if (!match) {24 process.stderr.write(chalk.red('Alphabet was not found\n'))25 process.exit(1)26}27let alphabet = match[0]2829function mutationFunction (phenotype) {30 let i = Math.floor(Math.random() * phenotype.alphabet.length)31 let j = Math.floor(Math.random() * phenotype.alphabet.length)32 return { alphabet: swapChars(phenotype.alphabet, i, j) }33}3435function swapChars (str, index1, index2) {36 let l = index1 < index2 ? index1 : index237 let h = index1 > index2 ? index1 : index238 return str.substring(0, l) +39 str[h] +40 str.substring(l + 1, h) +41 str[l] +42 str.substring(h + 1, str.length)43}4445function crossoverFunction (phenotypeA, phenotypeB) {46 let a = phenotypeA.alphabet47 let b = phenotypeB.alphabet48 let indexA = Math.floor(Math.random() * a.length / 2 + a.length / 2)49 let indexB = Math.floor(Math.random() + a.length / 2)50 let newStrA = a.substring(indexA, a.length)51 let newStrB = b.substring(0, indexB)52 return [53 { alphabet: addMissingCharacter(newStrA, b) },54 { alphabet: addMissingCharacter(newStrB, a) }55 ]56}5758function addMissingCharacter (str, proto) {59 let newStr = str60 for (let i = 0; i < proto.length; i++) {61 if (str.indexOf(proto[i]) === -1) {62 newStr += proto[i]63 }64 }65 return newStr66}6768function fitnessFunction (phenotype) {69 let file = js.replace(/[A-Za-z0-9-_]{30,}/, phenotype.alphabet)70 let size = gzipSize.sync(file)71 console.log(size, phenotype.alphabet)72 return -1 * size73}7475function sameAlphabet (a, b) {76 let ua = a.split('').sort().join('')77 let ub = b.split('').sort().join('')78 for (let i = 0; i < ua.length; i++) {79 if (ua[i] !== ub[i]) {80 return false81 }82 }83 return true84}8586const firstPhenotype = { alphabet }8788const geneticAlgorithm = geneticAlgorithmConstructor({89 mutationFunction,90 crossoverFunction,91 fitnessFunction,92 population: [firstPhenotype],93 populationSize: 500094})9596console.log('Starting with:', firstPhenotype.alphabet)97for (let i = 0; i < 100; i++) {98 geneticAlgorithm.evolve()99}100const best = geneticAlgorithm.best()101console.log('Finished with:', best.alphabet)102const isSame = sameAlphabet(firstPhenotype.alphabet, best.alphabet)103console.log(104 'Same set of characters:',105 isSame ? chalk.green('true') : chalk.red('false')106)107console.log(chalk.green(108 'Size new string:',109 gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, best.alphabet))110))111console.log(chalk.yellow(112 'Size old string:',113 gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, firstPhenotype.alphabet)))114)
deleted:test/alphabet-genetic.js-112 removed
1let spawnSync = require('child_process').spawnSync2let chalk = require('chalk')3let path = require('path')4let fs = require('fs')5let geneticAlgorithmConstructor = require('geneticalgorithm')6let gzipSize = require('gzip-size')78if (!process.argv[2]) {9 process.stderr.write(chalk.red('Pass a file with alphabet as argument\n'))10 process.exit(1)11}1213let input = path.join(process.cwd(), process.argv[2])14let output = path.join(__dirname, '..', 'compiled.js')1516spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])17let js = fs.readFileSync(output).toString()18fs.unlinkSync(output)1920let match = js.match(/[A-Za-z0-9-_]{30,}/)21if (!match) {22 process.stderr.write(chalk.red('Alphabet was not found\n'))23 process.exit(1)24}25let alphabet = match[0]2627function mutationFunction (phenotype) {28 let i = Math.floor(Math.random() * phenotype.alphabet.length)29 let j = Math.floor(Math.random() * phenotype.alphabet.length)3031 return {32 alphabet: swapChars(phenotype.alphabet, i, j)33 }34}3536function swapChars (str, index1, index2) {37 let l = index1 < index2 ? index1 : index238 let h = index1 > index2 ? index1 : index239 return str.substring(0, l) +40 str[h] +41 str.substring(l + 1, h) +42 str[l] +43 str.substring(h + 1, str.length)44}4546function crossoverFunction (phenotypeA, phenotypeB) {47 let alphabetA = phenotypeA.alphabet48 let alphabetB = phenotypeB.alphabet49 let indexA =50 Math.floor(Math.random() * alphabetA.length / 2 + alphabetA.length / 2)51 let indexB =52 Math.floor(Math.random() + alphabetA.length / 2)53 let newStrA = alphabetA.substring(indexA, alphabetA.length)54 let newStrB = alphabetB.substring(0, indexB)5556 return [57 { alphabet: addMissingCharacter(newStrA, alphabetB) },58 { alphabet: addMissingCharacter(newStrB, alphabetA) }59 ]60}6162function addMissingCharacter (str, proto) {63 let newStr = str64 for (let i = 0; i < proto.length; i++) {65 if (str.indexOf(proto[i]) === -1) {66 newStr += proto[i]67 }68 }69 return newStr70}7172function fitnessFunction (phenotype) {73 let file = js.replace(/[A-Za-z0-9-_]{30,}/, phenotype.alphabet)74 let size = gzipSize.sync(file)75 console.log(size, phenotype.alphabet)76 return -1 * size77}7879function sameAlphabet (a, b) {80 let ua = a.split('').sort().join('')81 let ub = b.split('').sort().join('')82 for (let i = 0; i < ua.length; i++) {83 if (ua[i] !== ub[i]) {84 return false85 }86 }87 return true88}8990const firstPhenotype = { alphabet }9192const geneticAlgorithm = geneticAlgorithmConstructor({93 mutationFunction,94 crossoverFunction,95 fitnessFunction,96 population: [firstPhenotype],97 populationSize: 500098})99100console.log('Starting with:', firstPhenotype.alphabet)101for (let i = 0; i < 100; i++) {102 geneticAlgorithm.evolve()103}104const best = geneticAlgorithm.best()105console.log('Finished with:', best.alphabet)106const isSame = sameAlphabet(firstPhenotype.alphabet, best.alphabet)107console.log('Same set of characters:',108 isSame ? chalk.green('true') : chalk.red('false'))109console.log(chalk.green('Size new string:',110 gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, best.alphabet))))111console.log(chalk.yellow('Size old string:',112 gzipSize.sync(js.replace(/[A-Za-z0-9-_]{30,}/, firstPhenotype.alphabet))))