Bläddra i källkod

Storages: sliglhtly changed behaviour of `getKeyOf` methods, added tests

sanex3339 8 år sedan
förälder
incheckning
e8f68171dd

+ 45 - 55
dist/index.js

@@ -90,7 +90,7 @@ module.exports =
 /******/ 	__webpack_require__.p = "";
 /******/
 /******/ 	// Load entry module and return exports
-/******/ 	return __webpack_require__(__webpack_require__.s = 149);
+/******/ 	return __webpack_require__(__webpack_require__.s = 150);
 /******/ })
 /************************************************************************/
 /******/ ([
@@ -776,14 +776,6 @@ var _from = __webpack_require__(45);
 
 var _from2 = _interopRequireDefault(_from);
 
-var _slicedToArray2 = __webpack_require__(35);
-
-var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
-
-var _getIterator2 = __webpack_require__(18);
-
-var _getIterator3 = _interopRequireDefault(_getIterator2);
-
 var _toConsumableArray2 = __webpack_require__(24);
 
 var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
@@ -798,7 +790,6 @@ var _createClass3 = _interopRequireDefault(_createClass2);
 
 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
-var _ = __webpack_require__(146);
 var JSFuck_1 = __webpack_require__(36);
 var RandomGeneratorUtils_1 = __webpack_require__(9);
 
@@ -859,40 +850,6 @@ var Utils = function () {
         value: function isCeilNumber(number) {
             return number % 1 === 0;
         }
-    }, {
-        key: "mapGetFirstKeyOf",
-        value: function mapGetFirstKeyOf(map, value) {
-            var _iteratorNormalCompletion = true;
-            var _didIteratorError = false;
-            var _iteratorError = undefined;
-
-            try {
-                for (var _iterator = (0, _getIterator3.default)(map), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
-                    var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
-                        key = _step$value[0],
-                        storageValue = _step$value[1];
-
-                    if (_.isEqual(value, storageValue)) {
-                        return key;
-                    }
-                }
-            } catch (err) {
-                _didIteratorError = true;
-                _iteratorError = err;
-            } finally {
-                try {
-                    if (!_iteratorNormalCompletion && _iterator.return) {
-                        _iterator.return();
-                    }
-                } finally {
-                    if (_didIteratorError) {
-                        throw _iteratorError;
-                    }
-                }
-            }
-
-            return null;
-        }
     }, {
         key: "strEnumify",
         value: function strEnumify(obj) {
@@ -2228,6 +2185,14 @@ var _map = __webpack_require__(12);
 
 var _map2 = _interopRequireDefault(_map);
 
+var _slicedToArray2 = __webpack_require__(35);
+
+var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
+
+var _getIterator2 = __webpack_require__(18);
+
+var _getIterator3 = _interopRequireDefault(_getIterator2);
+
 var _classCallCheck2 = __webpack_require__(0);
 
 var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
@@ -2242,7 +2207,6 @@ var tslib_1 = __webpack_require__(3);
 var inversify_1 = __webpack_require__(2);
 var Initializable_1 = __webpack_require__(10);
 var RandomGeneratorUtils_1 = __webpack_require__(9);
-var Utils_1 = __webpack_require__(14);
 var MapStorage = function () {
     function MapStorage() {
         (0, _classCallCheck3.default)(this, MapStorage);
@@ -2260,7 +2224,36 @@ var MapStorage = function () {
     }, {
         key: "getKeyOf",
         value: function getKeyOf(value) {
-            return Utils_1.Utils.mapGetFirstKeyOf(this.storage, value);
+            var _iteratorNormalCompletion = true;
+            var _didIteratorError = false;
+            var _iteratorError = undefined;
+
+            try {
+                for (var _iterator = (0, _getIterator3.default)(this.storage), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
+                    var _step$value = (0, _slicedToArray3.default)(_step.value, 2),
+                        key = _step$value[0],
+                        storageValue = _step$value[1];
+
+                    if (value === storageValue) {
+                        return key;
+                    }
+                }
+            } catch (err) {
+                _didIteratorError = true;
+                _iteratorError = err;
+            } finally {
+                try {
+                    if (!_iteratorNormalCompletion && _iterator.return) {
+                        _iterator.return();
+                    }
+                } finally {
+                    if (_didIteratorError) {
+                        throw _iteratorError;
+                    }
+                }
+            }
+
+            return null;
         }
     }, {
         key: "getLength",
@@ -2303,7 +2296,7 @@ var MapStorage = function () {
 }();
 tslib_1.__decorate([Initializable_1.initializable(), tslib_1.__metadata("design:type", String)], MapStorage.prototype, "storageId", void 0);
 tslib_1.__decorate([Initializable_1.initializable(), tslib_1.__metadata("design:type", _map2.default)], MapStorage.prototype, "storage", void 0);
-MapStorage = tslib_1.__decorate([inversify_1.injectable(), tslib_1.__metadata("design:paramtypes", [])], MapStorage);
+MapStorage = tslib_1.__decorate([inversify_1.injectable()], MapStorage);
 exports.MapStorage = MapStorage;
 
 /***/ }),
@@ -7826,7 +7819,8 @@ var ArrayStorage = function () {
     }, {
         key: "getKeyOf",
         value: function getKeyOf(value) {
-            return this.storage.indexOf(value);
+            var key = this.storage.indexOf(value);
+            return key >= 0 ? key : null;
         }
     }, {
         key: "getLength",
@@ -8343,12 +8337,7 @@ module.exports = require("events");
 module.exports = require("fs");
 
 /***/ }),
-/* 146 */
-/***/ (function(module, exports) {
-
-module.exports = require("lodash");
-
-/***/ }),
+/* 146 */,
 /* 147 */
 /***/ (function(module, exports) {
 
@@ -8361,7 +8350,8 @@ module.exports = require("mkdirp");
 module.exports = require("reflect-metadata");
 
 /***/ }),
-/* 149 */
+/* 149 */,
+/* 150 */
 /***/ (function(module, exports, __webpack_require__) {
 
 "use strict";

+ 2 - 2
package.json

@@ -47,7 +47,7 @@
     "@types/estree": "0.0.34",
     "@types/lodash": "4.14.50",
     "@types/mkdirp": "0.3.29",
-    "@types/mocha": "2.2.37",
+    "@types/mocha": "2.2.38",
     "@types/node": "7.0.0",
     "@types/sinon": "1.16.34",
     "@types/string-template": "1.0.2",
@@ -65,7 +65,7 @@
     "tslint": "4.3.1",
     "tslint-loader": "3.3.0",
     "typescript": "2.1.5",
-    "webpack": "2.2.0-rc.7",
+    "webpack": "2.2.0",
     "webpack-node-externals": "1.5.4"
   },
   "repository": {

+ 4 - 3
src/storages/ArrayStorage.ts

@@ -41,10 +41,11 @@ export abstract class ArrayStorage <T> implements IStorage <T> {
 
     /**
      * @param value
-     * @returns {string | number}
+     * @returns {number | null}
      */
-    public getKeyOf (value: T): string | number {
-        return this.storage.indexOf(value);
+    public getKeyOf (value: T): number | null {
+        const key: number = this.storage.indexOf(value);
+        return key >= 0 ? key : null;
     }
 
     /**

+ 7 - 2
src/storages/MapStorage.ts

@@ -5,7 +5,6 @@ import { IStorage } from '../interfaces/storages/IStorage';
 import { initializable } from '../decorators/Initializable';
 
 import { RandomGeneratorUtils } from '../utils/RandomGeneratorUtils';
-import { Utils } from '../utils/Utils';
 
 @injectable()
 export abstract class MapStorage <T> implements IStorage <T> {
@@ -40,7 +39,13 @@ export abstract class MapStorage <T> implements IStorage <T> {
      * @returns {string | number | null}
      */
     public getKeyOf (value: T): string | number | null {
-        return Utils.mapGetFirstKeyOf <string | number, T> (this.storage, value);
+        for (const [key, storageValue] of this.storage) {
+            if (value === storageValue) {
+                return key;
+            }
+        }
+
+        return null;
     }
 
     /**

+ 0 - 16
src/utils/Utils.ts

@@ -1,4 +1,3 @@
-import * as _ from 'lodash';
 import { JSFuck } from '../enums/JSFuck';
 
 import { RandomGeneratorUtils } from './RandomGeneratorUtils';
@@ -92,21 +91,6 @@ export class Utils {
         return number % 1 === 0;
     }
 
-    /**
-     * @param map
-     * @param value
-     * @returns {T | null}
-     */
-    public static mapGetFirstKeyOf <T, U> (map: Map <T, U>, value: U): T | null {
-        for (const [key, storageValue] of map) {
-            if (_.isEqual(value, storageValue)) {
-                return key;
-            }
-        }
-
-        return null;
-    }
-
     /**
      * @param obj
      * @returns {T}

+ 2 - 0
test/index.spec.ts

@@ -15,6 +15,8 @@ import './unit-tests/obfuscation-result/ObfuscationResult.spec';
 import './unit-tests/options/options-normalizer/OptionsNormalizer.spec';
 import './unit-tests/source-map-corrector/SourceMapCorrector.spec';
 import './unit-tests/stack-trace-analyzer/stack-trace-analyzer/StackTraceAnalyzer.spec';
+import './unit-tests/storages/ArrayStorage.spec';
+import './unit-tests/storages/MapStorage.spec';
 import './unit-tests/utils/crypt-utils/CryptUtils.spec';
 import './unit-tests/utils/random-generator-utils/RandomGeneratorUtils.spec';
 import './unit-tests/utils/utils/Utils.spec';

+ 126 - 0
test/unit-tests/storages/ArrayStorage.spec.ts

@@ -0,0 +1,126 @@
+import { assert } from 'chai';
+
+import { IStorage } from '../../../src/interfaces/storages/IStorage';
+
+import { ArrayStorage } from '../../../src/storages/ArrayStorage';
+
+class ConcreteStorage extends ArrayStorage <string> {
+    constructor () {
+        super();
+    }
+}
+
+describe('ArrayStorage', () => {
+    describe('initialize (...args: any[]): void', () => {
+        it('should throws an error when storage isn\'t initialized', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            assert.throws(() => storage.set(0, 'foo'), Error);
+        });
+    });
+
+    describe('getStorage (): T[]', () => {
+        it('should returns storage', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+
+            assert.instanceOf(storage.getStorage(), Array);
+        });
+    });
+
+    describe('get (key: number): T', () => {
+        it('should returns value from storage by key', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set(0, 'foo');
+
+            assert.equal(storage.get(0), 'foo');
+        });
+
+        it('should throw an error if value isn\'t exist', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+
+            assert.throws(() => storage.get(0), Error);
+        });
+    });
+
+    describe('getLength (): number', () => {
+        it('should returns length of storage', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set(0, 'foo');
+
+            assert.equal(storage.getLength(), 1);
+        });
+    });
+
+    describe('getKeyOf (value: T): number | null', () => {
+        it('should returns key of string value', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set(0, 'foo');
+
+            assert.equal(storage.getKeyOf('foo'), 0);
+        });
+
+        it('should returns key of object if objects in `set` and `get` are two linked objects', () => {
+            const storage: IStorage <Object> = new ConcreteStorage();
+            const object: Object = {
+                foo: 'bar'
+            };
+
+            storage.initialize();
+            storage.set(0, object);
+
+            assert.equal(storage.getKeyOf(object), 0);
+        });
+
+        it('should return `null` if objects in `set` and `get` are two equal objects', () => {
+            const storage: IStorage <Object> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set(0, {
+                foo: 'bar'
+            });
+
+            assert.equal(storage.getKeyOf({
+                foo: 'bar'
+            }), null);
+        });
+    });
+
+    describe('set (key: number, value: T): void', () => {
+        it('should set value to the storage', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set(0, 'foo');
+
+            assert.equal(storage.get(0), 'foo');
+            assert.equal(storage.getLength(), 1);
+        });
+    });
+
+    describe('mergeWith (storage: this, mergeId: boolean = false): void', () => {
+        it('should merge two storages', () => {
+            const storage1: IStorage <string> = new ConcreteStorage();
+            const storage2: IStorage <string> = new ConcreteStorage();
+
+            storage1.initialize();
+            storage1.set(0, 'foo');
+
+            storage2.initialize();
+            storage2.set(1, 'bar');
+
+            storage1.mergeWith(storage2, false);
+
+            assert.deepEqual(storage1.getStorage(), ['foo', 'bar']);
+        });
+    });
+});

+ 126 - 0
test/unit-tests/storages/MapStorage.spec.ts

@@ -0,0 +1,126 @@
+import { assert } from 'chai';
+
+import { IStorage } from '../../../src/interfaces/storages/IStorage';
+
+import { MapStorage } from '../../../src/storages/MapStorage';
+
+class ConcreteStorage extends MapStorage <string> {
+    constructor () {
+        super();
+    }
+}
+
+describe('MapStorage', () => {
+    describe('initialize (...args: any[]): void', () => {
+        it('should throws an error when storage isn\'t initialized', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            assert.throws(() => storage.set('foo', 'bar'), Error);
+        });
+    });
+
+    describe('getStorage (): Map <string | number, T>', () => {
+        it('should returns storage', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+
+            assert.instanceOf(storage.getStorage(), Map);
+        });
+    });
+
+    describe('get (key: string | number): T', () => {
+        it('should returns value from storage by key', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set('foo', 'bar');
+
+            assert.equal(storage.get('foo'), 'bar');
+        });
+
+        it('should throw an error if value isn\'t exist', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+
+            assert.throws(() => storage.get('foo'), Error);
+        });
+    });
+
+    describe('getLength (): number', () => {
+        it('should returns length of storage', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set('foo', 'bar');
+
+            assert.equal(storage.getLength(), 1);
+        });
+    });
+
+    describe('getKeyOf (value: T): string | number | null', () => {
+        it('should returns key of string value', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set('foo', 'bar');
+
+            assert.equal(storage.getKeyOf('bar'), 'foo');
+        });
+
+        it('should returns key of object if objects in `set` and `get` are two linked objects', () => {
+            const storage: IStorage <Object> = new ConcreteStorage();
+            const object: Object = {
+                bar: 'baz'
+            };
+
+            storage.initialize();
+            storage.set('foo', object);
+
+            assert.equal(storage.getKeyOf(object), 'foo');
+        });
+
+        it('should return `null` if objects in `set` and `get` are two equal objects', () => {
+            const storage: IStorage <Object> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set('foo', {
+                bar: 'baz'
+            });
+
+            assert.equal(storage.getKeyOf({
+                bar: 'baz'
+            }), null);
+        });
+    });
+
+    describe('set (key: string | number, value: T): void', () => {
+        it('should set value to the storage', () => {
+            const storage: IStorage <string> = new ConcreteStorage();
+
+            storage.initialize();
+            storage.set('foo', 'bar');
+
+            assert.equal(storage.get('foo'), 'bar');
+            assert.equal(storage.getLength(), 1);
+        });
+    });
+
+    describe('mergeWith (storage: this, mergeId: boolean = false): void', () => {
+        it('should merge two storages', () => {
+            const storage1: IStorage <string> = new ConcreteStorage();
+            const storage2: IStorage <string> = new ConcreteStorage();
+
+            storage1.initialize();
+            storage1.set('foo', 'bar');
+
+            storage2.initialize();
+            storage2.set('baz', 'quux');
+
+            storage1.mergeWith(storage2, false);
+
+            assert.deepEqual(Array.from(storage1.getStorage()), [['foo', 'bar'], ['baz', 'quux']]);
+        });
+    });
+});

+ 0 - 28
test/unit-tests/utils/utils/Utils.spec.ts

@@ -53,34 +53,6 @@ describe('Utils', () => {
         });
     });
 
-    describe('mapGetFirstKeyOf(map: Map <any, any>, value: any): any', () => {
-        it('should returns key of map item', () => {
-            const map: Map <any, any> = new Map();
-
-            map.set('number1', 1);
-            map.set('number2', 2);
-            map.set('number3', 2);
-
-            map.set('string1', 'foo');
-            map.set('string2', 'bar');
-
-            map.set('object1', {item: 'value'});
-            map.set('object2', {item: 'value'});
-            map.set({key: 'object'}, [1, 2, 3]);
-
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, 1), 'number1');
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, 2), 'number2');
-
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'foo'), 'string1');
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, 'bar'), 'string2');
-
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, {item: 'value'}), 'object1');
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, [1, 2, 3]), {key: 'object'});
-
-            assert.deepEqual(Utils.mapGetFirstKeyOf(map, 3), null);
-        });
-    });
-
     describe('stringRotate (string: string, times: number): string', () => {
         let string: string;