Browse Source

Rewrite to JS

Henry Jameson 6 years ago
parent
commit
53b9c770e6
3 changed files with 161 additions and 0 deletions
  1. 31
    0
      package.json
  2. 115
    0
      src/dict.js
  3. 15
    0
      src/index.js

+ 31
- 0
package.json View File

@@ -0,0 +1,31 @@
1
+{
2
+  "name": "weeapass",
3
+  "version": "1.0.0",
4
+  "description": "Weeaboo password generator",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "repository": {
10
+    "type": "git",
11
+    "url": "git+https://github.com/hjri/Weeapass.git"
12
+  },
13
+  "keywords": [
14
+    "password",
15
+    "generator"
16
+  ],
17
+  "author": "hj",
18
+  "license": "ISC",
19
+  "bugs": {
20
+    "url": "https://github.com/hjri/Weeapass/issues"
21
+  },
22
+  "homepage": "https://github.com/hjri/Weeapass#readme",
23
+  "dependencies": {
24
+    "chalk": "^1.1.1",
25
+    "chance": "^1.0.1",
26
+    "commander": "^2.9.0",
27
+    "lodash": "^4.6.1",
28
+    "weighted": "^0.3.0",
29
+    "weighted-random": "^0.1.0"
30
+  }
31
+}

+ 115
- 0
src/dict.js View File

@@ -0,0 +1,115 @@
1
+var _ = require('lodash'),
2
+    baked = false;
3
+    chance = new require('chance')(),
4
+    classes = [
5
+        'syllable',
6
+        'vowel-only',
7
+        'n',
8
+        'youon',
9
+        'syllable-youon'
10
+    ],
11
+    consonants = {
12
+        'b': 100,
13
+        'c': 75,
14
+        'd': 100,
15
+        'f': 50,
16
+        'g': 100,
17
+        'h': 50,
18
+        'j': 100,
19
+        'k': 100,
20
+        'l': 'r',
21
+        'm': 100,
22
+        'n': 75,
23
+        'p': 50,
24
+        'q': 0,
25
+        'r': 50,
26
+        's': 100,
27
+        't': 0,
28
+        'v': 25,
29
+        'w': 'v',
30
+        'x': 0,
31
+        'z': 50
32
+    },
33
+    vowels = ['a', 'i', 'e', 'o', 'u'],
34
+    youonConsonants = ['k', 's', 'c'],
35
+    generateFunctions = {
36
+        'syllable': function(vowel, consonant) {
37
+            return consonant + vowel;
38
+        },
39
+        'youon': function(vowel) {
40
+            return 'y' + vowel;
41
+        },
42
+        'n': function() {
43
+            return 'n';
44
+        },
45
+        'syllable-youon': function(vowel, consonant) {
46
+            return consonant + 'y' + vowel;
47
+        },
48
+        'vowel-only': function(vowel) {
49
+            return vowel;
50
+        }
51
+    },
52
+    bakeArrays = function(options) {
53
+        // Make reusable??
54
+        if (baked) return;
55
+        options = options || {};
56
+        consonants = _.each(consonants, function(v, k) {
57
+            if (typeof v === 'string') {
58
+                consonants[k] = consonants[v];
59
+            };
60
+        });
61
+
62
+        // make c2sh and c2t cross-compatible?
63
+        if (options.s2sh) {
64
+            consonants.sh = consonants.s;
65
+            consonants.ch = consonants.c;
66
+            delete consonants.s;
67
+            delete consonants.c;
68
+        }
69
+        if (options.c2t) {
70
+            consonants.t = consonants.c;
71
+            delete consonants.c;
72
+        }
73
+        options.engrish && delete consonants.l;
74
+        options.noW && delete consonants.w;
75
+        options.noV && delete consonants.v;
76
+        baked = true;
77
+    },
78
+    generateNextSyllable = function(previousSyllables) {
79
+        var classProbability = {
80
+            'syllable': 100,
81
+            'syllable-youon': 12,
82
+            'n': 25,
83
+            'vowel-only': 50,
84
+            'youon': 25
85
+        };
86
+        // TODO add '-' support?
87
+        if (_.last(previousSyllables) && _.last(previousSyllables).class === 'n') {
88
+            classProbability.n = 0;
89
+        }
90
+        classProbability['vowel-only'] = classProbability['vowel-only'] /
91
+            (_.filter(previousSyllables, function(syll) {
92
+                return syll.class === 'vowel';
93
+            }).length + 1);
94
+        var nextClass = chance.weighted( _.keys(classProbability), _.values(classProbability)),
95
+            consSet = _.includes(nextClass, 'youon') ? _.pick(consonants, youonConsonants) : consonants,
96
+            consonant = chance.weighted(_.keys(consSet),_.values(consSet)),
97
+            vowel = chance.pick(vowels),
98
+            next = generateFunctions[nextClass](vowel, consonant);
99
+
100
+        return {
101
+            class: nextClass,
102
+            text: next
103
+        };
104
+    },
105
+    word = [],
106
+    makeWord = function(options, length) {
107
+        bakeArrays(options);
108
+        var index = 0,
109
+            word = [];
110
+        for(index;index<length;index++){
111
+            word.push(generateNextSyllable(word));
112
+        }
113
+        return _(word).map(function(w){return w.text;}).join('');
114
+    };
115
+module.exports = makeWord;

+ 15
- 0
src/index.js View File

@@ -0,0 +1,15 @@
1
+/**
2
+ * Module dependencies.
3
+ */
4
+
5
+var program = require('commander');
6
+
7
+program
8
+    .version('1.0.0')
9
+    .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
10
+
11
+console.log('you ordered a pizza with:');
12
+if (program.peppers) console.log('  - peppers');
13
+if (program.pineapple) console.log('  - pineapple');
14
+if (program.bbqSauce) console.log('  - bbq');
15
+console.log('  - %s cheese', program.cheese);