Skip to content

Commit

Permalink
refactor: fix cr issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Sep 19, 2024
1 parent 5efc22e commit 57ebb2e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 73 deletions.
2 changes: 1 addition & 1 deletion __tests__/unit/color/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('color', function () {
};

// implement document defaultView getComputedStyle getPropertyValue in jsdom
mockGetComputedStyle = jest.spyOn(window, 'getComputedStyle').mockImplementation(
mockGetComputedStyle = jest.spyOn(document.defaultView!, 'getComputedStyle').mockImplementation(

Check warning on line 16 in __tests__/unit/color/index.spec.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion

Check warning on line 16 in __tests__/unit/color/index.spec.ts

View workflow job for this annotation

GitHub Actions / build

Forbidden non-null assertion
(el) =>
({
getPropertyValue: () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/unit/lodash/max.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('max', () => {
});

it('empty', () => {
expect(max([])).toBe(undefined);
expect(max([NaN])).toBe(undefined);
expect(max([1, 2, NaN])).toBe(undefined);
expect(max([])).toBe(-Infinity);
expect(max([NaN])).toBe(NaN);
expect(max([1, 2, NaN])).toBe(NaN);
});
});
2 changes: 1 addition & 1 deletion src/color/torgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function toRGBString(color: string): string {

iEl.style.color = color;

let rst = window.getComputedStyle(iEl, '').getPropertyValue('color');
let rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color');
const matches = RGB_REG.exec(rst) as string[];
const cArray: number[] = matches[1].split(/\s*,\s*/).map((s) => Number(s));

Expand Down
32 changes: 8 additions & 24 deletions src/lodash/max.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import isNil from './is-nil';

/**
* @param {Array} arr The array to iterate over.
* @return {*} Returns the maximum value.
* @example
*
* max([1, 2]);
* // => 2
*
* max([]);
* // => undefined
*
* const data = new Array(1250010).fill(1).map((d,idx) => idx);
*
* max(data);
* // => 1250010
* // Math.max(...data) will encounter "Maximum call stack size exceeded" error
*/
export default function max(arr: number[]): number | undefined {
const length = arr.length;
if (length === 0) return undefined;
let max = arr[0];
export default function max(arr: number[]): number {
let max = -Infinity;
const seg = 100000;

const isNonNumber = (value: number) => isNil(value) || isNaN(value);
if (isNonNumber(max)) return undefined;
if (arr.length === 0) return -Infinity;

for (let i = 1; i < length; i++) {
if (isNonNumber(arr[i])) return undefined;
if (arr[i] > max) max = arr[i];
for (let i = 0; i < arr.length; i += seg) {
max = Math.max(max, Math.max(...arr.slice(i, i + seg)));
if (max === Infinity) return Infinity;
else if (Number.isNaN(max)) return NaN;
}

return max;
Expand Down
81 changes: 37 additions & 44 deletions src/lodash/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
class FLRUCache<K, V> {
private cache: Map<K, V>;
private capacity: number;
function flru(max: number) {
let num, curr, prev;
const limit = max || 1;

constructor(capacity: number) {
this.capacity = Math.max(capacity, 1);
this.cache = new Map<K, V>();
}

// 获取缓存中的值,并更新使用顺序
get(key: K): V | undefined {
if (!this.cache.has(key)) {
return undefined;
function keep(key, value) {
if (++num > limit) {
prev = curr;
reset(1);
++num;
}

// 先删除再插入,以便更新顺序
const value = this.cache.get(key)!;
this.cache.delete(key);
this.cache.set(key, value);
return value;
curr[key] = value;
}

// 设置缓存值,更新使用顺序,并维护缓存大小
set(key: K, value: V): void {
if (this.cache.has(key)) {
// 如果 key 已存在,更新值并重新设置顺序
this.cache.delete(key);
} else if (this.cache.size >= this.capacity) {
// 容量已满,移除最久未使用的键值对
const firstKey = this.cache.keys().next().value;
this.cache.delete(firstKey);
}
this.cache.set(key, value);
function reset(isPartial?: number) {
num = 0;
curr = Object.create(null);
isPartial || (prev = Object.create(null));
}

// 检查是否存在指定的 key
has(key: K): boolean {
return this.cache.has(key);
}
reset();

// 获取当前缓存大小
size(): number {
return this.cache.size;
}

// 清空缓存
clear(): void {
this.cache.clear();
}
return {
clear: reset,
has: function (key) {
return curr[key] !== void 0 || prev[key] !== void 0;
},
get: function (key) {
var val = curr[key];
if (val !== void 0) return val;
if ((val = prev[key]) !== void 0) {
keep(key, val);
return val;
}
},
set: function (key, value) {
if (curr[key] !== void 0) {
curr[key] = value;
} else {
keep(key, value);
}
},
};
}

const CacheMap = new Map<Function, FLRUCache<any, any>>();
const CacheMap = new Map<Function, ReturnType<typeof flru>>();

/**
* 缓存函数的计算结果,避免重复计算
Expand All @@ -64,7 +57,7 @@ export default function memoize<T extends Function>(fn: T, resolver?: (...args:
const memoized = function (...args) {
// 使用方法构造 key,如果不存在 resolver,则直接取第一个参数作为 key
const key = resolver ? resolver.apply(this, args) : args[0];
if (!CacheMap.has(fn)) CacheMap.set(fn, new FLRUCache(maxSize));
if (!CacheMap.has(fn)) CacheMap.set(fn, flru(maxSize));
const cache = CacheMap.get(fn);

if (cache.has(key)) return cache.get(key);
Expand Down

0 comments on commit 57ebb2e

Please sign in to comment.