浏览代码

Replaced internal `getRandomInteger` function by `Chance` library `integer` method call

sanex3339 9 年之前
父节点
当前提交
7ecfee26b6

+ 31 - 7
dist/index.js

@@ -257,6 +257,7 @@ module.exports =
 	
 	function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 	
+	var chance_1 = __webpack_require__(39);
 	var JSFuck_1 = __webpack_require__(8);
 	
 	var Utils = function () {
@@ -304,9 +305,9 @@ module.exports =
 	            return (dec + Math.pow(radix, exponent)).toString(radix).substr(decToHexSliceValue).replace(Utils.hexRepetitiveZerosRegExp, '');
 	        }
 	    }, {
-	        key: 'getRandomInteger',
-	        value: function getRandomInteger(min, max) {
-	            return Math.round(Math.floor(Math.random() * (max - min + 1)) + min);
+	        key: 'getRandomGenerator',
+	        value: function getRandomGenerator() {
+	            return Utils.randomGenerator;
 	        }
 	    }, {
 	        key: 'getRandomVariableName',
@@ -316,7 +317,10 @@ module.exports =
 	            var rangeMinInteger = 10000,
 	                rangeMaxInteger = 99999999,
 	                prefix = '_0x';
-	            return '' + prefix + Utils.decToHex(Utils.getRandomInteger(rangeMinInteger, rangeMaxInteger)).substr(0, length);
+	            return '' + prefix + Utils.decToHex(Utils.getRandomGenerator().integer({
+	                min: rangeMinInteger,
+	                max: rangeMaxInteger
+	            })).substr(0, length);
 	        }
 	    }, {
 	        key: 'isInteger',
@@ -359,6 +363,7 @@ module.exports =
 	}();
 	
 	Utils.hexRepetitiveZerosRegExp = new RegExp('^(0{2,})+(?!$)', '');
+	Utils.randomGenerator = new chance_1.Chance();
 	exports.Utils = Utils;
 
 /***/ },
@@ -1216,7 +1221,10 @@ module.exports =
 	        key: 'appendNode',
 	        value: function appendNode(blockScopeNode) {
 	            var programBodyLength = blockScopeNode.body.length,
-	                randomIndex = Utils_1.Utils.getRandomInteger(0, programBodyLength);
+	                randomIndex = Utils_1.Utils.getRandomGenerator().integer({
+	                min: 0,
+	                max: programBodyLength
+	            });
 	            NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);
 	        }
 	    }, {
@@ -1279,7 +1287,10 @@ module.exports =
 	            var programBodyLength = blockScopeNode.body.length,
 	                randomIndex = 0;
 	            if (programBodyLength > 2) {
-	                randomIndex = Utils_1.Utils.getRandomInteger(programBodyLength / 2, programBodyLength - 1);
+	                randomIndex = Utils_1.Utils.getRandomGenerator().integer({
+	                    min: programBodyLength / 2,
+	                    max: programBodyLength - 1
+	                });
 	            }
 	            NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);
 	        }
@@ -1745,7 +1756,14 @@ module.exports =
 	
 	        _this.unicodeArrayName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH);
 	        _this.unicodeArrayTranslatorName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH);
-	        _this.unicodeArrayRotateValue = _this.options.get('rotateUnicodeArray') ? Utils_1.Utils.getRandomInteger(100, 500) : 0;
+	        if (_this.options.get('rotateUnicodeArray')) {
+	            _this.unicodeArrayRotateValue = Utils_1.Utils.getRandomGenerator().integer({
+	                min: 100,
+	                max: 500
+	            });
+	        } else {
+	            _this.unicodeArrayRotateValue = 0;
+	        }
 	        var unicodeArrayNode = new UnicodeArrayNode_1.UnicodeArrayNode(_this.unicodeArrayName, _this.unicodeArrayRotateValue, _this.options),
 	            unicodeArray = unicodeArrayNode.getNodeData();
 	        _this.nodes.set('unicodeArrayNode', unicodeArrayNode);
@@ -2443,6 +2461,12 @@ module.exports =
 	    wrapUnicodeArrayCalls: true
 	});
 
+/***/ },
+/* 39 */
+/***/ function(module, exports) {
+
+	module.exports = require("chance");
+
 /***/ }
 /******/ ]);
 //# sourceMappingURL=index.js.map

+ 2 - 1
package.json

@@ -13,8 +13,9 @@
   ],
   "main": "dist/index.js",
   "dependencies": {
-    "esprima": "^2.7.2",
+    "chance": "^1.0.3",
     "escodegen": "^1.8.0",
+    "esprima": "^2.7.2",
     "estraverse": "^4.2.0"
   },
   "devDependencies": {

+ 18 - 6
src/Utils.ts

@@ -1,3 +1,5 @@
+import { Chance } from 'chance';
+
 import { JSFuck } from './enums/JSFuck';
 
 export class Utils {
@@ -6,6 +8,11 @@ export class Utils {
      */
     private static hexRepetitiveZerosRegExp: RegExp = new RegExp('^(0{2,})+(?!$)', '');
 
+    /**
+     * @type {Chance.Chance}
+     */
+    private static randomGenerator: Chance.Chance = new Chance();
+
     /**
      * @param array
      * @param searchElement
@@ -65,12 +72,10 @@ export class Utils {
     }
 
     /**
-     * @param min
-     * @param max
-     * @returns {number}
+     * @returns {Chance.Chance}
      */
-    public static getRandomInteger(min: number, max: number): number {
-        return Math.round(Math.floor(Math.random() * (max - min + 1)) + min);
+    public static getRandomGenerator (): Chance.Chance {
+        return Utils.randomGenerator;
     }
 
     /**
@@ -82,7 +87,14 @@ export class Utils {
             rangeMaxInteger: number = 99999999,
             prefix: string = '_0x';
 
-        return `${prefix}${(Utils.decToHex(Utils.getRandomInteger(rangeMinInteger, rangeMaxInteger))).substr(0, length)}`;
+        return `${prefix}${(
+            Utils.decToHex(
+                Utils.getRandomGenerator().integer({
+                    min: rangeMinInteger,
+                    max: rangeMaxInteger
+                })
+            )
+        ).substr(0, length)}`;
     }
 
     /**

+ 4 - 1
src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts

@@ -32,7 +32,10 @@ export class DebugProtectionFunctionNode extends Node {
      */
     public appendNode (blockScopeNode: TBlockScopeNode): void {
         let programBodyLength: number = blockScopeNode.body.length,
-            randomIndex: number = Utils.getRandomInteger(0, programBodyLength);
+            randomIndex: number = Utils.getRandomGenerator().integer({
+                min: 0,
+                max: programBodyLength
+            });
 
         NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);
     }

+ 4 - 1
src/custom-nodes/self-defending-nodes/SelfDefendingUnicodeNode.ts

@@ -38,7 +38,10 @@ export class SelfDefendingUnicodeNode extends Node {
             randomIndex: number = 0;
 
         if (programBodyLength > 2) {
-            randomIndex = Utils.getRandomInteger(programBodyLength / 2, programBodyLength - 1);
+            randomIndex = Utils.getRandomGenerator().integer({
+                min: programBodyLength / 2,
+                max: programBodyLength - 1
+            });
         }
 
         NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);

+ 8 - 1
src/node-groups/UnicodeArrayNodesGroup.ts

@@ -29,7 +29,14 @@ export class UnicodeArrayNodesGroup extends NodesGroup {
     constructor (options: IOptions) {
         super(options);
 
-        this.unicodeArrayRotateValue = this.options.get('rotateUnicodeArray') ? Utils.getRandomInteger(100, 500) : 0;
+        if (this.options.get('rotateUnicodeArray')) {
+            this.unicodeArrayRotateValue = Utils.getRandomGenerator().integer({
+                min: 100,
+                max: 500
+            });
+        } else {
+            this.unicodeArrayRotateValue = 0;
+        }
 
         let unicodeArrayNode: UnicodeArrayNode = new UnicodeArrayNode(
                 this.unicodeArrayName,

+ 1 - 0
typings.json

@@ -3,6 +3,7 @@
   "version": false,
   "ambientDependencies": {
     "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#6834f97fb33561a3ad40695084da2b660efaee29",
+    "chance": "github:DefinitelyTyped/DefinitelyTyped/chance/chance.d.ts",
     "estree": "github:DefinitelyTyped/DefinitelyTyped/estree/estree.d.ts",
     "esprima": "github:DefinitelyTyped/DefinitelyTyped/esprima/esprima.d.ts",
     "escodegen": "github:DefinitelyTyped/DefinitelyTyped/escodegen/escodegen.d.ts",

+ 1 - 0
typings/browser.d.ts

@@ -1,3 +1,4 @@
+/// <reference path="browser/ambient/chance/index.d.ts" />
 /// <reference path="browser/ambient/escodegen/index.d.ts" />
 /// <reference path="browser/ambient/esprima/index.d.ts" />
 /// <reference path="browser/ambient/estraverse/index.d.ts" />

+ 224 - 0
typings/browser/ambient/chance/index.d.ts

@@ -0,0 +1,224 @@
+// Generated by typings
+// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/chance/chance.d.ts
+// Type definitions for Chance 0.7.3
+// Project: http://chancejs.com
+// Definitions by: Chris Bowdon <https://github.com/cbowdon/>
+// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
+
+declare namespace Chance {
+
+    interface ChanceStatic {
+        (): Chance
+        (seed: number): Chance
+        (generator: () => any): Chance
+
+        new(): Chance;
+        new(seed: number): Chance;
+        new(generator: () => any): Chance;
+    }
+
+    interface Chance {
+
+        // Basics
+        bool(opts?: Options): boolean;
+        character(opts?: Options): string;
+        floating(opts?: Options): number;
+        integer(opts?: Options): number;
+        natural(opts?: Options): number;
+        string(opts?: Options): string;
+
+        // Text
+        paragraph(opts?: Options): string;
+        sentence(opts?: Options): string;
+        syllable(opts?: Options): string;
+        word(opts?: Options): string;
+
+        // Person
+        age(opts?: Options): number;
+        birthday(): Date;
+        birthday(opts?: Options): Date|string;
+        cpf(): string;
+        first(opts?: Options): string;
+        last(opts?: Options): string;
+        name(opts?: Options): string;
+        name_prefix(opts?: Options): string;
+        name_suffix(opts?: Options): string;
+        prefix(opts?: Options): string;
+        ssn(opts?: Options): string;
+        suffix(opts?: Options): string;
+
+        // Mobile
+        android_id(): string;
+        apple_token(): string;
+        bb_pin(): string;
+        wp7_anid(): string;
+        wp8_anid2(): string;
+
+        // Web
+        color(opts?: Options): string;
+        domain(opts?: Options): string;
+        email(opts?: Options): string;
+        fbid(): string;
+        google_analytics(): string;
+        hashtag(): string;
+        ip(): string;
+        ipv6(): string;
+        klout(): string;
+        tld(): string;
+        twitter(): string;
+        url(opts?: Options): string;
+
+        // Location
+        address(opts?: Options): string;
+        altitude(opts?: Options): number;
+        areacode(): string;
+        city(): string;
+        coordinates(opts?: Options): string;
+        country(opts?: Options): string;
+        depth(opts?: Options): number;
+        geohash(opts?: Options): string;
+        latitude(opts?: Options): number;
+        longitude(opts?: Options): number;
+        phone(opts?: Options): string;
+        postal(): string;
+        province(opts?: Options): string;
+        state(opts?: Options): string;
+        street(opts?: Options): string;
+        zip(opts?: Options): string;
+
+        // Time
+        ampm(): string;
+        date(): Date;
+        date(opts: DateOptions): Date|string;
+        hammertime(): number;
+        hour(opts?: Options): number;
+        millisecond(): number;
+        minute(): number;
+        month(): string;
+        month(opts: Options): Month;
+        second(): number;
+        timestamp(): number;
+        year(opts?: Options): string;
+
+        // Finance
+        cc(opts?: Options): string;
+        cc_type(): string;
+        cc_type(opts: Options): string|CreditCardType;
+        currency(): Currency;
+        currency_pair(): [ Currency, Currency ];
+        dollar(opts?: Options): string;
+        exp(): string;
+        exp(opts: Options): string|CreditCardExpiration;
+        exp_month(opts?: Options): string;
+        exp_year(opts?: Options): string;
+
+        // Helpers
+        capitalize(str: string): string;
+        mixin(desc: MixinDescriptor): any;
+        pad(num: number, width: number, padChar?: string): string;
+        pick<T>(arr: T[]): T;
+        pick<T>(arr: T[], count: number): T[];
+        set: Setter;
+        shuffle<T>(arr: T[]): T[];
+
+        // Miscellaneous
+        d4(): number;
+        d6(): number;
+        d8(): number;
+        d10(): number;
+        d12(): number;
+        d20(): number;
+        d30(): number;
+        d100(): number;
+        guid(): string;
+        hash(opts?: Options): string;
+        n<T>(generator: () => T, count: number, opts?: Options): T[];
+        normal(opts?: Options): number;
+        radio(opts?: Options): string;
+        rpg(dice: string): number[];
+        rpg(dice: string, opts?: Options): number[]|number;
+        tv(opts?: Options): string;
+        unique<T>(generator: () => T, count: number, opts?: Options): T[];
+        weighted<T>(values: T[], weights: number[]): T;
+
+        // "Hidden"
+        cc_types(): CreditCardType[];
+        mersenne_twister(seed?: number): any; // API return type not defined in docs
+        months(): Month[];
+        name_prefixes(): Name[];
+        provinces(): Name[];
+        states(): Name[];
+        street_suffix(): Name;
+        street_suffixes(): Name[];
+    }
+
+    // A more rigorous approach might be to produce
+    // the correct options interfaces for each method
+    interface Options { [id: string]: any; }
+
+    interface DateOptions {
+        string?: boolean;
+        american?: boolean;
+        year?: number;
+        month?: number;
+        day?: number;
+    }
+
+    interface Month {
+        name: string;
+        short_name: string;
+        numeric: string;
+    }
+
+    interface CreditCardType {
+        name: string;
+        short_name: string;
+        prefix: string;
+        length: number;
+    }
+
+    interface Currency {
+        code: string;
+        name: string;
+    }
+
+    interface CreditCardExpiration {
+        month: string;
+        year: string;
+    }
+
+    interface MixinDescriptor { [id: string]: () => any; }
+
+    interface Setter {
+        (key: 'firstNames', values: string[]): any;
+        (key: 'lastNames', values: string[]): any;
+        (key: 'provinces', values: string[]): any;
+        (key: 'us_states_and_dc', values: string[]): any;
+        (key: 'territories', values: string[]): any;
+        (key: 'armed_forces', values: string[]): any;
+        (key: 'street_suffixes', values: string[]): any;
+        (key: 'months', values: string[]): any;
+        (key: 'cc_types', values: string[]): any;
+        (key: 'currency_types', values: string[]): any;
+        <T>(key: string, values: T[]): any;
+    }
+
+    interface Name {
+        name: string;
+        abbreviation: string;
+    }
+}
+
+// window.chance
+declare var chance: Chance.Chance;
+declare var Chance: Chance.ChanceStatic;
+
+// import Chance = require('chance');
+declare module 'chance' {
+    interface ExportedChance extends Chance.ChanceStatic {
+        Chance: ExportedChance;
+    }
+    var Chance: ExportedChance;
+
+    export = Chance;
+}

+ 1 - 0
typings/main.d.ts

@@ -1,3 +1,4 @@
+/// <reference path="main/ambient/chance/index.d.ts" />
 /// <reference path="main/ambient/escodegen/index.d.ts" />
 /// <reference path="main/ambient/esprima/index.d.ts" />
 /// <reference path="main/ambient/estraverse/index.d.ts" />

+ 224 - 0
typings/main/ambient/chance/index.d.ts

@@ -0,0 +1,224 @@
+// Generated by typings
+// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/chance/chance.d.ts
+// Type definitions for Chance 0.7.3
+// Project: http://chancejs.com
+// Definitions by: Chris Bowdon <https://github.com/cbowdon/>
+// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
+
+declare namespace Chance {
+
+    interface ChanceStatic {
+        (): Chance
+        (seed: number): Chance
+        (generator: () => any): Chance
+
+        new(): Chance;
+        new(seed: number): Chance;
+        new(generator: () => any): Chance;
+    }
+
+    interface Chance {
+
+        // Basics
+        bool(opts?: Options): boolean;
+        character(opts?: Options): string;
+        floating(opts?: Options): number;
+        integer(opts?: Options): number;
+        natural(opts?: Options): number;
+        string(opts?: Options): string;
+
+        // Text
+        paragraph(opts?: Options): string;
+        sentence(opts?: Options): string;
+        syllable(opts?: Options): string;
+        word(opts?: Options): string;
+
+        // Person
+        age(opts?: Options): number;
+        birthday(): Date;
+        birthday(opts?: Options): Date|string;
+        cpf(): string;
+        first(opts?: Options): string;
+        last(opts?: Options): string;
+        name(opts?: Options): string;
+        name_prefix(opts?: Options): string;
+        name_suffix(opts?: Options): string;
+        prefix(opts?: Options): string;
+        ssn(opts?: Options): string;
+        suffix(opts?: Options): string;
+
+        // Mobile
+        android_id(): string;
+        apple_token(): string;
+        bb_pin(): string;
+        wp7_anid(): string;
+        wp8_anid2(): string;
+
+        // Web
+        color(opts?: Options): string;
+        domain(opts?: Options): string;
+        email(opts?: Options): string;
+        fbid(): string;
+        google_analytics(): string;
+        hashtag(): string;
+        ip(): string;
+        ipv6(): string;
+        klout(): string;
+        tld(): string;
+        twitter(): string;
+        url(opts?: Options): string;
+
+        // Location
+        address(opts?: Options): string;
+        altitude(opts?: Options): number;
+        areacode(): string;
+        city(): string;
+        coordinates(opts?: Options): string;
+        country(opts?: Options): string;
+        depth(opts?: Options): number;
+        geohash(opts?: Options): string;
+        latitude(opts?: Options): number;
+        longitude(opts?: Options): number;
+        phone(opts?: Options): string;
+        postal(): string;
+        province(opts?: Options): string;
+        state(opts?: Options): string;
+        street(opts?: Options): string;
+        zip(opts?: Options): string;
+
+        // Time
+        ampm(): string;
+        date(): Date;
+        date(opts: DateOptions): Date|string;
+        hammertime(): number;
+        hour(opts?: Options): number;
+        millisecond(): number;
+        minute(): number;
+        month(): string;
+        month(opts: Options): Month;
+        second(): number;
+        timestamp(): number;
+        year(opts?: Options): string;
+
+        // Finance
+        cc(opts?: Options): string;
+        cc_type(): string;
+        cc_type(opts: Options): string|CreditCardType;
+        currency(): Currency;
+        currency_pair(): [ Currency, Currency ];
+        dollar(opts?: Options): string;
+        exp(): string;
+        exp(opts: Options): string|CreditCardExpiration;
+        exp_month(opts?: Options): string;
+        exp_year(opts?: Options): string;
+
+        // Helpers
+        capitalize(str: string): string;
+        mixin(desc: MixinDescriptor): any;
+        pad(num: number, width: number, padChar?: string): string;
+        pick<T>(arr: T[]): T;
+        pick<T>(arr: T[], count: number): T[];
+        set: Setter;
+        shuffle<T>(arr: T[]): T[];
+
+        // Miscellaneous
+        d4(): number;
+        d6(): number;
+        d8(): number;
+        d10(): number;
+        d12(): number;
+        d20(): number;
+        d30(): number;
+        d100(): number;
+        guid(): string;
+        hash(opts?: Options): string;
+        n<T>(generator: () => T, count: number, opts?: Options): T[];
+        normal(opts?: Options): number;
+        radio(opts?: Options): string;
+        rpg(dice: string): number[];
+        rpg(dice: string, opts?: Options): number[]|number;
+        tv(opts?: Options): string;
+        unique<T>(generator: () => T, count: number, opts?: Options): T[];
+        weighted<T>(values: T[], weights: number[]): T;
+
+        // "Hidden"
+        cc_types(): CreditCardType[];
+        mersenne_twister(seed?: number): any; // API return type not defined in docs
+        months(): Month[];
+        name_prefixes(): Name[];
+        provinces(): Name[];
+        states(): Name[];
+        street_suffix(): Name;
+        street_suffixes(): Name[];
+    }
+
+    // A more rigorous approach might be to produce
+    // the correct options interfaces for each method
+    interface Options { [id: string]: any; }
+
+    interface DateOptions {
+        string?: boolean;
+        american?: boolean;
+        year?: number;
+        month?: number;
+        day?: number;
+    }
+
+    interface Month {
+        name: string;
+        short_name: string;
+        numeric: string;
+    }
+
+    interface CreditCardType {
+        name: string;
+        short_name: string;
+        prefix: string;
+        length: number;
+    }
+
+    interface Currency {
+        code: string;
+        name: string;
+    }
+
+    interface CreditCardExpiration {
+        month: string;
+        year: string;
+    }
+
+    interface MixinDescriptor { [id: string]: () => any; }
+
+    interface Setter {
+        (key: 'firstNames', values: string[]): any;
+        (key: 'lastNames', values: string[]): any;
+        (key: 'provinces', values: string[]): any;
+        (key: 'us_states_and_dc', values: string[]): any;
+        (key: 'territories', values: string[]): any;
+        (key: 'armed_forces', values: string[]): any;
+        (key: 'street_suffixes', values: string[]): any;
+        (key: 'months', values: string[]): any;
+        (key: 'cc_types', values: string[]): any;
+        (key: 'currency_types', values: string[]): any;
+        <T>(key: string, values: T[]): any;
+    }
+
+    interface Name {
+        name: string;
+        abbreviation: string;
+    }
+}
+
+// window.chance
+declare var chance: Chance.Chance;
+declare var Chance: Chance.ChanceStatic;
+
+// import Chance = require('chance');
+declare module 'chance' {
+    interface ExportedChance extends Chance.ChanceStatic {
+        Chance: ExportedChance;
+    }
+    var Chance: ExportedChance;
+
+    export = Chance;
+}