README
CoolStrings
Technically cool strings generator.
Overview
This module generates random strings which we can treat as cool.
String coolness?
Yeah. String coolness defined as set of logic rules:
- First character of cool word can't differs from the edges of alphabet by more than half of alphabet length:
- a > ⌈ b / 2 ⌉ or abs(a - b) > ⌈ b / 2 ⌉, where a is first character, b is a count of alphabet characters.
- Each following character of cool string differs from previous by alphabet length divided by 2:
- abs(an-1 - an) > ⌈ b / 2 ⌉, where a is a string and b is a count of alphabet characters.
Why?
Well, there's kinda weird expain for this...
Did you know that there is Russian phrase "крутой склон", which means slope with angle more than 45°. So, its whole phrase, but we will use it in another point for our explanation: word "крутой" means "cool" in translation (I know that in phrase it refers to acute angle). 45° is half of an acute angle, that's the rule for differences. I know it's weird and hard to understand, so there are some images:
API
Class
This module exports a class. You can use it as following:
new CoolStrings(marvelousParameters)
Where:
marvelousParameters = {
alphabet: [ 'a', 'b' ... ], // Alphabet array
min: 3 // Minimum characters in a cool word
};
Properties
defaultParameters
This property returns default marvelousParameters
.
Methods
new
Returns a Promise
.
When it resolved it returns a cool string.
newCrypto(timeout)
Returns a Promise
.
The same as new
but every time string generates randomly until it reaches a minimum.
timeout
- time in milliseconds when Promise
will be forcibly resolved if no cool enough word generated.
setParameter(parameter, value)
Returns a marvelousParameters
.
Allows to set a parameter for marvelousParameters
.
parameter
- min
or alphabet
value
- new value.
isCoolString(string, marvelousParameters)
Returns a Boolean
.
It checks whatever provided string is cool enough.
string
- a string which pretends to be cool.
genRandomCharacter()
Internal function
Returns a Promise
.
When it resolved it returns a random index for alphabet.
genCoolCharacter(prevChar)
Internal function
Returns a Promise
.
When it resolved it returns a cool random index for alphabet.
prevChar
- index for alphabet for a previous character.
genCoolFirstCharacter()
Internal function
Returns a Promise
.
When it resolved it returns a cool random index for alphabet.
charOfAlphabet(char, marvelousParameters)
Internal function
Returns a Number
.
It returns a cool index of providing character for a provided alphabet.
char
- index for alphabet for a character.
isCoolCharacter(char, parameter, marvelousParameters)
Internal function
Returns a Boolean
.
It checks whatever is providing character cool enough for the previous.
char
- index for alphabet for a character.
parameter
- index for alphabet for a previous character or true
if it need to check whatever is provided character cool enough to became the first one.
isAlphabetCoolEnough(alphabet)
Internal function
Returns a Boolean
.
It checks whatever is providing alphabet is cool enough.
alphabet
- the same as marvelousParameters.alphabet
.
parametersIsFine(parameters)
Internal function
Returns a Boolean
.
It checks whatever is providing parameters is cool enough.
parameters
- the same as marvelousParameters
.
Usage
(async function() {
try {
const CoolStrings = require('coolstrings');
const coolString = new CoolStrings();
coolString.setParameter('alphabet', ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
coolString.setParameter('min', 15);
console.log(await coolString.new()); // 818291808070919
console.log(await coolString.newCrypto()); // { timeout: true, time: [ 2, 501548637 ], string: '8192817' }
console.log(coolString.isCoolString('D.Va'.split('.').join('')), coolString.defaultParameters); // true
} catch (e) {
console.error(e);
}
})();
const coolString = new (require('coolstrings'))();
var ic = (...args) => { return coolString.isCoolString.call(coolString, ...args) }
console.log(ic('Dva')); // true