mirror of
https://github.com/gabemart/hipku.git
synced 2025-01-18 12:05:35 +00:00
Allow index.js to run in browser or in node
This commit is contained in:
parent
cf65e8bdfd
commit
c4e7fd3598
1517
dist/hipku.js
vendored
1517
dist/hipku.js
vendored
File diff suppressed because it is too large
Load Diff
1
dist/hipku.min.js
vendored
1
dist/hipku.min.js
vendored
File diff suppressed because one or more lines are too long
250
index.js
250
index.js
@ -8,10 +8,10 @@
|
|||||||
;
|
;
|
||||||
var Hipku = (function() {
|
var Hipku = (function() {
|
||||||
/*
|
/*
|
||||||
** ##############
|
** ##############
|
||||||
** Public Methods
|
** Public Methods
|
||||||
** ##############
|
** ##############
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Object holds all public methods and is returned by the module
|
** Object holds all public methods and is returned by the module
|
||||||
@ -24,39 +24,39 @@ var publicMethods = {};
|
|||||||
var encode = function(ip) {
|
var encode = function(ip) {
|
||||||
var ipv6, decimalOctetArray, factoredOctetArray, encodedWordArray,
|
var ipv6, decimalOctetArray, factoredOctetArray, encodedWordArray,
|
||||||
haikuText;
|
haikuText;
|
||||||
|
|
||||||
ipv6 = ipIsIpv6(ip);
|
ipv6 = ipIsIpv6(ip);
|
||||||
decimalOctetArray = splitIp(ip, ipv6);
|
decimalOctetArray = splitIp(ip, ipv6);
|
||||||
factoredOctetArray = factorOctets(decimalOctetArray, ipv6);
|
factoredOctetArray = factorOctets(decimalOctetArray, ipv6);
|
||||||
encodedWordArray = encodeWords(factoredOctetArray, ipv6);
|
encodedWordArray = encodeWords(factoredOctetArray, ipv6);
|
||||||
haikuText = writeHaiku(encodedWordArray, ipv6);
|
haikuText = writeHaiku(encodedWordArray, ipv6);
|
||||||
|
|
||||||
return haikuText;
|
return haikuText;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Public method to decode haiku into IP Addresses
|
** Public method to decode haiku into IP Addresses
|
||||||
*/
|
*/
|
||||||
var decode = function(haiku) {
|
var decode = function(haiku) {
|
||||||
var wordArray, ipv6, factorArray, octetArray, ipString;
|
var wordArray, ipv6, factorArray, octetArray, ipString;
|
||||||
|
|
||||||
wordArray = splitHaiku(haiku);
|
wordArray = splitHaiku(haiku);
|
||||||
ipv6 = haikuIsIpv6(wordArray);
|
ipv6 = haikuIsIpv6(wordArray);
|
||||||
factorArray = getFactors(wordArray, ipv6);
|
factorArray = getFactors(wordArray, ipv6);
|
||||||
octetArray = getOctets(factorArray, ipv6);
|
octetArray = getOctets(factorArray, ipv6);
|
||||||
ipString = getIpString(octetArray, ipv6);
|
ipString = getIpString(octetArray, ipv6);
|
||||||
|
|
||||||
return ipString;
|
return ipString;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Attach the public methods to the return object
|
** Attach the public methods to the return object
|
||||||
*/
|
*/
|
||||||
publicMethods.encode = encode;
|
publicMethods.encode = encode;
|
||||||
publicMethods.decode = decode;
|
publicMethods.decode = decode;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** #############################
|
** #############################
|
||||||
** Helper functions for encoding
|
** Helper functions for encoding
|
||||||
** #############################
|
** #############################
|
||||||
*/
|
*/
|
||||||
@ -72,11 +72,11 @@ function ipIsIpv6(ip) {
|
|||||||
|
|
||||||
function splitIp(ip, ipv6) {
|
function splitIp(ip, ipv6) {
|
||||||
var octetArray, separator, v6Base, numOctets, decimalOctetArray;
|
var octetArray, separator, v6Base, numOctets, decimalOctetArray;
|
||||||
|
|
||||||
octetArray = [];
|
octetArray = [];
|
||||||
decimalOctetArray = [];
|
decimalOctetArray = [];
|
||||||
v6Base = 16;
|
v6Base = 16;
|
||||||
|
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
separator = ':';
|
separator = ':';
|
||||||
numOctets = 8;
|
numOctets = 8;
|
||||||
@ -84,20 +84,20 @@ function splitIp(ip, ipv6) {
|
|||||||
separator = '.';
|
separator = '.';
|
||||||
numOctets = 4;
|
numOctets = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Remove newline and space characters
|
** Remove newline and space characters
|
||||||
*/
|
*/
|
||||||
ip = ip.replace(/[\n\ ]/g, '');
|
ip = ip.replace(/[\n\ ]/g, '');
|
||||||
octetArray = ip.split(separator);
|
octetArray = ip.split(separator);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If IPv6 address is in abbreviated format, we need to replace missing octets with 0
|
** If IPv6 address is in abbreviated format, we need to replace missing octets with 0
|
||||||
*/
|
*/
|
||||||
if (octetArray.length < numOctets) {
|
if (octetArray.length < numOctets) {
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
var numMissingOctets = (numOctets - octetArray.length);
|
var numMissingOctets = (numOctets - octetArray.length);
|
||||||
|
|
||||||
octetArray = padOctets(octetArray, numMissingOctets);
|
octetArray = padOctets(octetArray, numMissingOctets);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Formatting error in IP Address input.' +
|
throw new Error('Formatting error in IP Address input.' +
|
||||||
@ -107,97 +107,97 @@ function splitIp(ip, ipv6) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Conter IPv6 addresses from hex to decimal
|
** Conter IPv6 addresses from hex to decimal
|
||||||
*/
|
*/
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
for (var i = 0; i < octetArray.length; i++) {
|
for (var i = 0; i < octetArray.length; i++) {
|
||||||
decimalOctetArray[i] = parseInt(octetArray[i], v6Base);
|
decimalOctetArray[i] = parseInt(octetArray[i], v6Base);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
decimalOctetArray = octetArray;
|
decimalOctetArray = octetArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
return decimalOctetArray;
|
return decimalOctetArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If IPv6 is abbreviated, pad with appropriate number of 0 octets
|
** If IPv6 is abbreviated, pad with appropriate number of 0 octets
|
||||||
*/
|
*/
|
||||||
function padOctets(octetArray, numMissingOctets) {
|
function padOctets(octetArray, numMissingOctets) {
|
||||||
var paddedOctet, aLength;
|
var paddedOctet, aLength;
|
||||||
|
|
||||||
paddedOctet = 0;
|
paddedOctet = 0;
|
||||||
aLength = octetArray.length;
|
aLength = octetArray.length;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If the first or last octets are blank, zero them
|
** If the first or last octets are blank, zero them
|
||||||
*/
|
*/
|
||||||
if (octetArray[0] === '') {
|
if (octetArray[0] === '') {
|
||||||
octetArray[0] = paddedOctet;
|
octetArray[0] = paddedOctet;
|
||||||
}
|
}
|
||||||
if (octetArray[aLength - 1] === '') {
|
if (octetArray[aLength - 1] === '') {
|
||||||
octetArray[aLength - 1] = paddedOctet;
|
octetArray[aLength - 1] = paddedOctet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check the rest of the array for blank octets and pad as needed
|
** Check the rest of the array for blank octets and pad as needed
|
||||||
*/
|
*/
|
||||||
for (var i = 0; i < aLength; i++) {
|
for (var i = 0; i < aLength; i++) {
|
||||||
if (octetArray[i] === '') {
|
if (octetArray[i] === '') {
|
||||||
octetArray[i] = paddedOctet;
|
octetArray[i] = paddedOctet;
|
||||||
|
|
||||||
for (var j = 0; j < numMissingOctets; j++) {
|
for (var j = 0; j < numMissingOctets; j++) {
|
||||||
octetArray.splice(i, 0, paddedOctet);
|
octetArray.splice(i, 0, paddedOctet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return octetArray;
|
return octetArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Convert each decimal octet into a factor of the divisor (16 or 256)
|
** Convert each decimal octet into a factor of the divisor (16 or 256)
|
||||||
** and a remainder
|
** and a remainder
|
||||||
*/
|
*/
|
||||||
function factorOctets(octetArray, ipv6) {
|
function factorOctets(octetArray, ipv6) {
|
||||||
var divisor, factoredOctetArray;
|
var divisor, factoredOctetArray;
|
||||||
|
|
||||||
factoredOctetArray = [];
|
factoredOctetArray = [];
|
||||||
|
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
divisor = 256;
|
divisor = 256;
|
||||||
} else {
|
} else {
|
||||||
divisor = 16;
|
divisor = 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < octetArray.length; i++) {
|
for (var i = 0; i < octetArray.length; i++) {
|
||||||
var octetValue, factor1, factor2;
|
var octetValue, factor1, factor2;
|
||||||
|
|
||||||
octetValue = octetArray[i];
|
octetValue = octetArray[i];
|
||||||
|
|
||||||
factor1 = octetArray[i] % divisor;
|
factor1 = octetArray[i] % divisor;
|
||||||
octetValue = octetValue - factor1;
|
octetValue = octetValue - factor1;
|
||||||
factor2 = octetValue / divisor;
|
factor2 = octetValue / divisor;
|
||||||
|
|
||||||
factoredOctetArray.push(factor2);
|
factoredOctetArray.push(factor2);
|
||||||
factoredOctetArray.push(factor1);
|
factoredOctetArray.push(factor1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return factoredOctetArray;
|
return factoredOctetArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
function encodeWords(factorArray, ipv6) {
|
function encodeWords(factorArray, ipv6) {
|
||||||
var key, encodedWordArray;
|
var key, encodedWordArray;
|
||||||
|
|
||||||
encodedWordArray = [];
|
encodedWordArray = [];
|
||||||
key = getKey(ipv6);
|
key = getKey(ipv6);
|
||||||
|
|
||||||
for (var i = 0; i < factorArray.length; i++) {
|
for (var i = 0; i < factorArray.length; i++) {
|
||||||
var dict;
|
var dict;
|
||||||
|
|
||||||
dict = key[i];
|
dict = key[i];
|
||||||
encodedWordArray[i] = dict[factorArray[i]];
|
encodedWordArray[i] = dict[factorArray[i]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return encodedWordArray;
|
return encodedWordArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +205,7 @@ function encodeWords(factorArray, ipv6) {
|
|||||||
/*
|
/*
|
||||||
** Return an array of dictionaries representing the correct word
|
** Return an array of dictionaries representing the correct word
|
||||||
** order for the haiku
|
** order for the haiku
|
||||||
*/
|
*/
|
||||||
function getKey(ipv6) {
|
function getKey(ipv6) {
|
||||||
var key;
|
var key;
|
||||||
|
|
||||||
@ -236,37 +236,37 @@ function getKey(ipv6) {
|
|||||||
plantNouns,
|
plantNouns,
|
||||||
plantVerbs ];
|
plantVerbs ];
|
||||||
}
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeHaiku(wordArray, ipv6) {
|
function writeHaiku(wordArray, ipv6) {
|
||||||
var octet, schemaResults, schema, nonWords, haiku;
|
var octet, schemaResults, schema, nonWords, haiku;
|
||||||
|
|
||||||
octet = 'OCTET'; // String to place in schema to show word slots
|
octet = 'OCTET'; // String to place in schema to show word slots
|
||||||
schemaResults = getSchema(ipv6, octet);
|
schemaResults = getSchema(ipv6, octet);
|
||||||
schema = schemaResults[0];
|
schema = schemaResults[0];
|
||||||
nonWords = schemaResults[1];
|
nonWords = schemaResults[1];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Replace each instance of 'octet' in the schema with a word from
|
** Replace each instance of 'octet' in the schema with a word from
|
||||||
** the encoded word array
|
** the encoded word array
|
||||||
*/
|
*/
|
||||||
for (var i = 0; i < wordArray.length; i++) {
|
for (var i = 0; i < wordArray.length; i++) {
|
||||||
for (var j = 0; j < schema.length; j++) {
|
for (var j = 0; j < schema.length; j++) {
|
||||||
if (schema[j] === octet) {
|
if (schema[j] === octet) {
|
||||||
schema[j] = wordArray[i];
|
schema[j] = wordArray[i];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Capitalize appropriate words
|
** Capitalize appropriate words
|
||||||
*/
|
*/
|
||||||
schema = capitalizeHaiku(schema);
|
schema = capitalizeHaiku(schema);
|
||||||
haiku = schema.join('');
|
haiku = schema.join('');
|
||||||
|
|
||||||
return haiku;
|
return haiku;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,7 +325,7 @@ function getSchema(ipv6, octet) {
|
|||||||
*/
|
*/
|
||||||
for (var i = 1; i < schema.length; i++) {
|
for (var i = 1; i < schema.length; i++) {
|
||||||
var insertSpace = true;
|
var insertSpace = true;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If the next entry is a nonWord, don't add a space
|
** If the next entry is a nonWord, don't add a space
|
||||||
*/
|
*/
|
||||||
@ -334,7 +334,7 @@ function getSchema(ipv6, octet) {
|
|||||||
insertSpace = false;
|
insertSpace = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If the previous entry is a newLine, don't add a space
|
** If the previous entry is a newLine, don't add a space
|
||||||
*/
|
*/
|
||||||
@ -353,156 +353,156 @@ function getSchema(ipv6, octet) {
|
|||||||
|
|
||||||
function capitalizeHaiku(haikuArray) {
|
function capitalizeHaiku(haikuArray) {
|
||||||
var period = '.';
|
var period = '.';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Always capitalize the first word
|
** Always capitalize the first word
|
||||||
*/
|
*/
|
||||||
haikuArray[0] = capitalizeWord(haikuArray[0]);
|
haikuArray[0] = capitalizeWord(haikuArray[0]);
|
||||||
|
|
||||||
for (var i = 1; i < haikuArray.length; i++) {
|
for (var i = 1; i < haikuArray.length; i++) {
|
||||||
|
|
||||||
if (haikuArray[i] === period && i + 2 < haikuArray.length) {
|
if (haikuArray[i] === period && i + 2 < haikuArray.length) {
|
||||||
/*
|
/*
|
||||||
** If the current entry is a period then the next entry will be
|
** If the current entry is a period then the next entry will be
|
||||||
** a newLine or a space, so check two positions ahead and
|
** a newLine or a space, so check two positions ahead and
|
||||||
** capitalize that entry, so long as it's a word
|
** capitalize that entry, so long as it's a word
|
||||||
*/
|
*/
|
||||||
haikuArray[i + 2] = capitalizeWord(haikuArray[i + 2]);
|
haikuArray[i + 2] = capitalizeWord(haikuArray[i + 2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return haikuArray;
|
return haikuArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
function capitalizeWord(word) {
|
function capitalizeWord(word) {
|
||||||
word = word.substring(0,1).toUpperCase() +
|
word = word.substring(0,1).toUpperCase() +
|
||||||
word.substring(1, word.length);
|
word.substring(1, word.length);
|
||||||
|
|
||||||
return word;
|
return word;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** #############################
|
** #############################
|
||||||
** Helper functions for decoding
|
** Helper functions for decoding
|
||||||
** #############################
|
** #############################
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function splitHaiku(haiku) {
|
function splitHaiku(haiku) {
|
||||||
var wordArray;
|
var wordArray;
|
||||||
|
|
||||||
haiku = haiku.toLowerCase();
|
haiku = haiku.toLowerCase();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Replace newline characters with spaces
|
** Replace newline characters with spaces
|
||||||
*/
|
*/
|
||||||
haiku = haiku.replace(/\n/g, ' ');
|
haiku = haiku.replace(/\n/g, ' ');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Remove anything that's not a letter, a space or a dash
|
** Remove anything that's not a letter, a space or a dash
|
||||||
*/
|
*/
|
||||||
haiku = haiku.replace(/[^a-z\ -]/g, '');
|
haiku = haiku.replace(/[^a-z\ -]/g, '');
|
||||||
wordArray = haiku.split(' ');
|
wordArray = haiku.split(' ');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Remove any blank entries
|
** Remove any blank entries
|
||||||
*/
|
*/
|
||||||
for (var i = 0; i < wordArray.length; i++) {
|
for (var i = 0; i < wordArray.length; i++) {
|
||||||
if (wordArray[i] === '') {
|
if (wordArray[i] === '') {
|
||||||
wordArray.splice(i, 1);
|
wordArray.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return wordArray;
|
return wordArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
function haikuIsIpv6(wordArray) {
|
function haikuIsIpv6(wordArray) {
|
||||||
var ipv6, key, dict;
|
var ipv6, key, dict;
|
||||||
|
|
||||||
key = getKey(false);
|
key = getKey(false);
|
||||||
dict = key[0];
|
dict = key[0];
|
||||||
ipv6 = true;
|
ipv6 = true;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Compare each word in the haiku against each word in the first
|
** Compare each word in the haiku against each word in the first
|
||||||
** dictionary defined in the IPv4 key. If there's a match, the
|
** dictionary defined in the IPv4 key. If there's a match, the
|
||||||
** current haiku is IPv4. If not, IPv6.
|
** current haiku is IPv4. If not, IPv6.
|
||||||
*/
|
*/
|
||||||
for (var i = 0; i < wordArray.length; i++) {
|
for (var i = 0; i < wordArray.length; i++) {
|
||||||
var currentWord = wordArray[i];
|
var currentWord = wordArray[i];
|
||||||
|
|
||||||
for (var j = 0; j < dict.length; j++) {
|
for (var j = 0; j < dict.length; j++) {
|
||||||
if (currentWord === dict[j]) {
|
if (currentWord === dict[j]) {
|
||||||
ipv6 = false;
|
ipv6 = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ipv6 === false) {
|
if (ipv6 === false) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ipv6;
|
return ipv6;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Return an array of factors and remainders for each encoded
|
** Return an array of factors and remainders for each encoded
|
||||||
** octet-value
|
** octet-value
|
||||||
*/
|
*/
|
||||||
function getFactors(wordArray, ipv6) {
|
function getFactors(wordArray, ipv6) {
|
||||||
var key, factorArray, wordArrayPosition;
|
var key, factorArray, wordArrayPosition;
|
||||||
|
|
||||||
key = getKey(ipv6);
|
key = getKey(ipv6);
|
||||||
factorArray = [];
|
factorArray = [];
|
||||||
wordArrayPosition = 0;
|
wordArrayPosition = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Get the first dictionary from the key. Check the first entry in
|
** Get the first dictionary from the key. Check the first entry in
|
||||||
** the encoded word array to see if it's in that dictionary. If it
|
** the encoded word array to see if it's in that dictionary. If it
|
||||||
** is, store the dictionary offset and move onto the next dictionary
|
** is, store the dictionary offset and move onto the next dictionary
|
||||||
** and the next word in the encoded words array. If there isn't a
|
** and the next word in the encoded words array. If there isn't a
|
||||||
** match, keep the same dictionary but check the next word in the
|
** match, keep the same dictionary but check the next word in the
|
||||||
** array. Keep going till we have an offset for each dictionary in
|
** array. Keep going till we have an offset for each dictionary in
|
||||||
** the key.
|
** the key.
|
||||||
*/
|
*/
|
||||||
for (var i = 0; i < key.length; i++) {
|
for (var i = 0; i < key.length; i++) {
|
||||||
var result, factor, newPosition;
|
var result, factor, newPosition;
|
||||||
|
|
||||||
result = [];
|
result = [];
|
||||||
result = getFactorFromWord(key[i], key.length,
|
result = getFactorFromWord(key[i], key.length,
|
||||||
wordArray, wordArrayPosition);
|
wordArray, wordArrayPosition);
|
||||||
factor = result[0];
|
factor = result[0];
|
||||||
newPosition = result[1];
|
newPosition = result[1];
|
||||||
wordArrayPosition = newPosition;
|
wordArrayPosition = newPosition;
|
||||||
|
|
||||||
factorArray.push(factor);
|
factorArray.push(factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
return factorArray;
|
return factorArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFactorFromWord(dict, maxLength, words, position) {
|
function getFactorFromWord(dict, maxLength, words, position) {
|
||||||
var factor = null;
|
var factor = null;
|
||||||
|
|
||||||
for (var j = 0; j < dict.length; j++) {
|
for (var j = 0; j < dict.length; j++) {
|
||||||
var dictEntryLength, wordToCheck;
|
var dictEntryLength, wordToCheck;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Get the number of words in the dictionary entry
|
** Get the number of words in the dictionary entry
|
||||||
*/
|
*/
|
||||||
dictEntryLength = dict[j].split(' ').length;
|
dictEntryLength = dict[j].split(' ').length;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** build a string to compare against the dictionary entry
|
** build a string to compare against the dictionary entry
|
||||||
** by joining the appropriate number of wordArray entries
|
** by joining the appropriate number of wordArray entries
|
||||||
*/
|
*/
|
||||||
wordToCheck =
|
wordToCheck =
|
||||||
words.slice(position, position + dictEntryLength);
|
words.slice(position, position + dictEntryLength);
|
||||||
wordToCheck = wordToCheck.join(' ');
|
wordToCheck = wordToCheck.join(' ');
|
||||||
|
|
||||||
if (dict[j] === wordToCheck) {
|
if (dict[j] === wordToCheck) {
|
||||||
factor = j;
|
factor = j;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** If the dictionary entry word count is greater than one,
|
** If the dictionary entry word count is greater than one,
|
||||||
** increment the position counter by the difference to
|
** increment the position counter by the difference to
|
||||||
@ -512,9 +512,9 @@ function getFactorFromWord(dict, maxLength, words, position) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
position = position + 1;
|
position = position + 1;
|
||||||
|
|
||||||
if (factor === null) {
|
if (factor === null) {
|
||||||
if (position >= maxLength) {
|
if (position >= maxLength) {
|
||||||
/*
|
/*
|
||||||
@ -522,15 +522,15 @@ function getFactorFromWord(dict, maxLength, words, position) {
|
|||||||
** all necessary dictionaries, so throw an error
|
** all necessary dictionaries, so throw an error
|
||||||
*/
|
*/
|
||||||
throw new Error('Decoding error: one or more dictionary words' +
|
throw new Error('Decoding error: one or more dictionary words' +
|
||||||
'missing from input haiku');
|
'missing from input haiku');
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
** Couldn't find the current word in the current dictionary,
|
** Couldn't find the current word in the current dictionary,
|
||||||
** try the next word
|
** try the next word
|
||||||
*/
|
*/
|
||||||
return getFactorFromWord(dict, maxLength, words, position);
|
return getFactorFromWord(dict, maxLength, words, position);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
** Found the word - return the dictionary offset and the new
|
** Found the word - return the dictionary offset and the new
|
||||||
** word array position
|
** word array position
|
||||||
@ -541,42 +541,42 @@ function getFactorFromWord(dict, maxLength, words, position) {
|
|||||||
|
|
||||||
function getOctets(factorArray, ipv6) {
|
function getOctets(factorArray, ipv6) {
|
||||||
var octetArray, multiplier;
|
var octetArray, multiplier;
|
||||||
|
|
||||||
octetArray = [];
|
octetArray = [];
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
multiplier = 256;
|
multiplier = 256;
|
||||||
} else {
|
} else {
|
||||||
multiplier = 16;
|
multiplier = 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < factorArray.length; i = i + 2) {
|
for (var i = 0; i < factorArray.length; i = i + 2) {
|
||||||
var factor1, factor2, octet;
|
var factor1, factor2, octet;
|
||||||
|
|
||||||
factor1 = factorArray[i];
|
factor1 = factorArray[i];
|
||||||
factor2 = factorArray[i + 1];
|
factor2 = factorArray[i + 1];
|
||||||
octet = (factor1 * multiplier) + factor2;
|
octet = (factor1 * multiplier) + factor2;
|
||||||
|
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
octet = octet.toString(16);
|
octet = octet.toString(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
octetArray.push(octet);
|
octetArray.push(octet);
|
||||||
}
|
}
|
||||||
|
|
||||||
return octetArray;
|
return octetArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIpString(octetArray, ipv6) {
|
function getIpString(octetArray, ipv6) {
|
||||||
var ipString, separator;
|
var ipString, separator;
|
||||||
|
|
||||||
ipString = '';
|
ipString = '';
|
||||||
|
|
||||||
if (ipv6) {
|
if (ipv6) {
|
||||||
separator = ':';
|
separator = ':';
|
||||||
} else {
|
} else {
|
||||||
separator = '.';
|
separator = '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < octetArray.length; i++) {
|
for (var i = 0; i < octetArray.length; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
ipString += separator;
|
ipString += separator;
|
||||||
@ -588,7 +588,7 @@ function getIpString(octetArray, ipv6) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** ############
|
** ############
|
||||||
** Dictionaries
|
** Dictionaries
|
||||||
** ############
|
** ############
|
||||||
*/
|
*/
|
||||||
@ -634,7 +634,7 @@ animalColors = ['beige',
|
|||||||
'pink',
|
'pink',
|
||||||
'red',
|
'red',
|
||||||
'white'];
|
'white'];
|
||||||
|
|
||||||
animalNouns = ['ape',
|
animalNouns = ['ape',
|
||||||
'bear',
|
'bear',
|
||||||
'crow',
|
'crow',
|
||||||
@ -668,7 +668,7 @@ animalVerbs = ['aches',
|
|||||||
'thrives',
|
'thrives',
|
||||||
'wakes',
|
'wakes',
|
||||||
'yawns'];
|
'yawns'];
|
||||||
|
|
||||||
natureAdjectives = ['ancient',
|
natureAdjectives = ['ancient',
|
||||||
'barren',
|
'barren',
|
||||||
'blazing',
|
'blazing',
|
||||||
@ -685,7 +685,7 @@ natureAdjectives = ['ancient',
|
|||||||
'serene',
|
'serene',
|
||||||
'sunlit',
|
'sunlit',
|
||||||
'wind-swept'];
|
'wind-swept'];
|
||||||
|
|
||||||
natureNouns = ['canyon',
|
natureNouns = ['canyon',
|
||||||
'clearing',
|
'clearing',
|
||||||
'desert',
|
'desert',
|
||||||
@ -719,7 +719,7 @@ plantNouns = ['autumn colors',
|
|||||||
'water lillies',
|
'water lillies',
|
||||||
'willow branches',
|
'willow branches',
|
||||||
'yellowwood leaves'];
|
'yellowwood leaves'];
|
||||||
|
|
||||||
plantVerbs = ['blow',
|
plantVerbs = ['blow',
|
||||||
'crunch',
|
'crunch',
|
||||||
'dance',
|
'dance',
|
||||||
@ -1254,7 +1254,7 @@ nouns = ['ants',
|
|||||||
'wraiths',
|
'wraiths',
|
||||||
'wrens',
|
'wrens',
|
||||||
'yaks'];
|
'yaks'];
|
||||||
|
|
||||||
verbs = ['aid',
|
verbs = ['aid',
|
||||||
'arm',
|
'arm',
|
||||||
'awe',
|
'awe',
|
||||||
@ -1511,7 +1511,11 @@ verbs = ['aid',
|
|||||||
'taunt',
|
'taunt',
|
||||||
'teach',
|
'teach',
|
||||||
'tend'];
|
'tend'];
|
||||||
|
|
||||||
module.exports = publicMethods;
|
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||||
|
module.exports = publicMethods;
|
||||||
|
} else {
|
||||||
|
return publicMethods;
|
||||||
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user