dictionary

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/es6-achieve/dictionary

说明

模拟ES6Map散列表数据结构

用法

dictionary.ts
dictionary.test.ts
dictionary.ts
import { Dictionary } from "./ddzy/utility/algorithm/es6-achieve/dictionary";
const map = new Dictionary();
const arr1 = [1, 2, 3];
const obj1 = {};
const func1 = function () { };
// ! 添加
map.set(arr1, 123);
map.set(obj1, '123323');
map.set(arr1, 456);
map.set(obj1, '23235235235');
map.set({ name: 'duan', age: 21 }, []);
map.set(func1, func1);
map.set(func1, 'it is function');
map.set(1, 2);
map.set({ name: 'duan', age: 21 }, []);
// ! 获取
// console.log(map.get(arr1));
// console.log(map.get(obj1));
// console.log(map.get({ name: 'duan', age: 21 }));
// console.log(map.get(func1));
// console.log(map.get(1));
// ! 移除
// console.log(map.delete(arr1));
// console.log(map.delete(obj1));
// console.log(map.delete({ name: 'duan', age: 21 }));
// console.log(map.delete(1));
// console.log(map);
// ! 元素个数
// console.log(map.size());
// ! 遍历
// map.traversal(function(value, key) {
// console.log({ key, value });
// });
dictionary.test.ts
describe('ES6Achieve.Dictionary tests', () => {
const map = new ES6Achieve.Dictionary();
test('Dictionary should work friendly', () => {
const str1 = 'duanzhaoyang';
const num1 = 19980808;
const bool1 = true;
const arr1 = [1, 2, 3];
const obj1 = { skill: 'program' };
const func1 = function () { };
const obj2 = { name: 'duan', age: 21 };
// map.set()
map.set(str1, str1);
map.set(num1, num1);
map.set(bool1, bool1);
map.set(arr1, arr1);
map.set(obj1, obj1);
map.set(func1, func1);
expect(map.size()).toBe(6);
// map.set() -> for twice
map.set(bool1, false);
map.set(obj1, obj2);
expect(map.size()).toBe(6);
// map.set() => for three times
map.set([], []);
expect(map.size()).toBe(7);
// map.get()
expect(map.get(arr1)).toBe(arr1);
expect(map.get(obj1)).toBe(obj2);
expect(map.size()).toBe(7);
// map.delete()
expect(map.delete(bool1)).toBeTruthy();
expect(map.size()).toBe(6);
// map.traversal()
map.traversal(function (value, key) {
expect(this).toBe(map);
if (key === bool1) {
return false;
}
});
});
});