Created
August 15, 2016 03:47
-
-
Save eanplatter/4b3618ac738d512dba2bb3336bbc4aac to your computer and use it in GitHub Desktop.
LED style number conversion in JavaScript / Node.
This file contains hidden or 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
const argv = require('process').argv | |
/** | |
* numbers - an object which lists out the ascii art of each number. | |
* It's kind of weird that they key for each object is a string, and that | |
* the string is a number. Not conventional but convenient as it allows you | |
* to reference `numbers['0']` to get the property on the numbers object. | |
*/ | |
const numbers = { | |
'0': { | |
ascii: [ | |
' _____ ', | |
'/ _ \\', | |
'| | | |', | |
'| | | |', | |
'| |_| |', | |
'\\_____/', | |
], | |
}, | |
'1': { | |
ascii: [ | |
' __ ', | |
' /_ | ', | |
' | | ', | |
' | | ', | |
' | | ', | |
' |_| ' | |
], | |
}, | |
'2': { | |
ascii: [ | |
' ___ ', | |
' |__ \\ ', | |
' ) |', | |
' / / ', | |
' / /_ ', | |
' |____|', | |
], | |
}, | |
'3': { | |
ascii: [ | |
' ____ ', | |
' |___ \\ ', | |
' __) |', | |
' |__ < ', | |
' ___) |', | |
' |____/ ', | |
], | |
}, | |
'4': { | |
ascii: [ | |
' _ _ ', | |
' | || | ', | |
' | || |_ ', | |
' |__ _|', | |
' | | ', | |
' |_| ', | |
], | |
}, | |
'5': { | |
ascii: [ | |
' _____ ', | |
' | ____|', | |
' | |__ ', | |
' |___ \\ ', | |
' ___) |', | |
' |____/ ', | |
], | |
}, | |
'6': { | |
ascii: [ | |
' __ ', | |
' / / ', | |
' / /_ ', | |
' | \'_ \\ ', | |
' | (_) |', | |
' \\___/ ', | |
], | |
}, | |
'7': { | |
ascii: [ | |
' ______ ', | |
' |____ |', | |
' / / ', | |
' / / ', | |
' / / ', | |
' /_/ ', | |
], | |
}, | |
'8': { | |
ascii: [ | |
' ___ ', | |
' / _ \\ ', | |
' | (_) |', | |
' > _ < ', | |
' | (_) |', | |
' \\___/ ', | |
], | |
}, | |
'9': { | |
ascii: [ | |
' ___ ', | |
' / _ \\ ', | |
' | (_) |', | |
' \\__, |', | |
' / / ', | |
' /_/ ', | |
], | |
}, | |
} | |
/** | |
* formatData - takes in a string, removes non-numbers, and returns | |
* the ascii art of each number. | |
* | |
* @param disp - the "display" numbers. | |
* @returns {Array} | |
*/ | |
function formatData (disp) { | |
return disp.split('') | |
.map(n => { | |
if (numbers[n]) { | |
return numbers[n].ascii | |
} else { | |
return null | |
} | |
}) | |
.filter(item => item) | |
} | |
/** | |
* melt - takes in an array of equally lengthed arrays... if that makes any sense, | |
* it then melts the 2nd dimension arrays together. | |
* into a single string. | |
* | |
* @param arr - 2d array of strings. example: [['a', 'b'],['c', 'd']] | |
* @returns {Array} - example: ['ac', 'bd'] | |
*/ | |
function melt (arr) { | |
const newArr = [] | |
/** | |
* forge - the recursively called method which does the actual | |
* mixing of the elements together. | |
* It lives in here so it can access the "newArr" variable. | |
* | |
* @param arr - 2d array of strings. example: [['a', 'b'],['c', 'd']] | |
* @returns {itself? lmao} | |
*/ | |
function forge (arr) { | |
let str = '' | |
if (arr[0].length < 1) { | |
return | |
} | |
for (let i = 0; i < arr.length; i++) { | |
str = str + arr[i][0] | |
arr[i] = arr[i].slice(1) | |
} | |
newArr.push(str) | |
return forge(arr) | |
} | |
forge(arr) | |
return newArr | |
} | |
/** | |
* executeDisplay - takes in the string, formats it, melts it, and maps over it to log it. | |
*/ | |
const executeDisplay = (disp) => melt(formatData(disp)).map(item => console.log(item)) | |
executeDisplay(process.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment