Skip to content

Commit

Permalink
feat(codition): save code
Browse files Browse the repository at this point in the history
  • Loading branch information
VicKun4937 committed Jun 11, 2024
1 parent 5e96825 commit 17bf4c6
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 57 deletions.
63 changes: 28 additions & 35 deletions packages/condition/src/_tests_/test.date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,98 @@ import { dateM1, dateM10, dateM11, dateM12, dateM2, dateM3, dateM4, dateM5, date
describe('date grouping test', () => {
it('dateM1 test', () => {
const date = new Date();
date.setMonth(1, 1);
expect(dateM1(date)).eq(true);
});
});

describe('date grouping test', () => {
it('dateM1 test', () => {
const date = new Date();
date.setMonth(1, 1);
date.setMonth(0, 1);
expect(dateM1(date)).toBe(true);
});

it('dateM2 test', () => {
const date = new Date();
date.setMonth(2, 1);
date.setMonth(1, 1);
expect(dateM2(date)).toBe(true);
});

it('dateM3 test', () => {
const date = new Date();
date.setMonth(3, 1);
date.setMonth(2, 1);
expect(dateM3(date)).toBe(true);
});

it('dateM4 test', () => {
const date = new Date();
date.setMonth(4, 1);
date.setMonth(3, 1);
expect(dateM4(date)).toBe(true);
});

it('dateM5 test', () => {
const date = new Date();
date.setMonth(5, 1);
date.setMonth(4, 1);
expect(dateM5(date)).toBe(true);
});

it('dateM6 test', () => {
const date = new Date();
date.setMonth(6, 1);
date.setMonth(5, 1);
expect(dateM6(date)).toBe(true);
});

it('dateM7 test', () => {
const date = new Date();
date.setMonth(7, 1);
date.setMonth(6, 1);
expect(dateM7(date)).toBe(true);
});

it('dateM8 test', () => {
const date = new Date();
date.setMonth(8, 1);
date.setMonth(7, 1);
expect(dateM8(date)).toBe(true);
});

it('dateM9 test', () => {
const date = new Date();
date.setMonth(9, 1);
date.setMonth(8, 1);
expect(dateM9(date)).toBe(true);
});

it('dateM10 test', () => {
const date = new Date();
date.setMonth(10, 1);
date.setMonth(9, 1);
expect(dateM10(date)).toBe(true);
});

it('dateM11 test', () => {
const date = new Date();
date.setMonth(11, 1);
date.setMonth(10, 1);
expect(dateM11(date)).toBe(true);
});

it('dateM12 test', () => {
const date = new Date();
date.setMonth(12, 1);
date.setMonth(11, 1);
expect(dateM12(date)).toBe(true);
});

it('dateQ1 test', () => {
const date = new Date();
date.setMonth(0, 1);
expect(dateQ1(date)).toBe(true);
});

it('dateQ2 test', () => {
const date = new Date();
date.setMonth(3, 1);
expect(dateQ2(date)).toBe(true);
});

it('dateQ3 test', () => {
const date = new Date();
date.setMonth(6, 1);
expect(dateQ3(date)).toBe(true);
});

it('dateQ4 test', () => {
const date = new Date();
date.setMonth(9, 1);
expect(dateQ4(date)).toBe(true);
});
});

describe('spacial date grouping test', () => {
it('spacial dateM1 test', () => {
const date = new Date();
date.setMonth(0, 31);
expect(dateM1(date)).toBe(true);
});
it('spacial dateM1 test', () => {
const date = new Date();
date.setMonth(0, 31);
date.setMonth(1);
// in this case, the February 31 is not a valid date, so the date change to March 3
expect(dateM2(date)).toBe(false);
expect(dateM3(date)).toBe(true);
});
});
1 change: 1 addition & 0 deletions packages/condition/src/_tests_/test.dateGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('dateGroup test', () => {
hour: 1,
dateGroupCompareType: DateGroupCompareTypeEnum.hour,
};

expect(groupByHour(input1[0], expected1)).toEqual(true);
expect(groupByHour(input1[1], expected1)).toEqual(false);
expect(groupByHour(input1[2], expected1)).toEqual(false);
Expand Down
73 changes: 73 additions & 0 deletions packages/condition/src/_tests_/test.dynamic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import { above, below, getBottomN, getTopN, thisWeek, today, tomorrow, yesterday } from '../condition/dynamicGenerator';

describe('dynamic test', () => {
it('above text', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const avg = arr.reduce((a, b) => a + b) / arr.length;

expect(above(1, avg)).toBe(false);
expect(above(6, avg)).toBe(true);
});
it('below text', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const avg = arr.reduce((a, b) => a + b) / arr.length;

expect(below(1, avg)).toBe(true);
expect(below(6, avg)).toBe(false);
});
it('getTopN test', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

expect(getTopN(arr, 5, 4)).toMatchObject(false);
expect(getTopN(arr, 5, 6)).toMatchObject(true);
});
it('getBottomN test', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

expect(getBottomN(arr, 5, 4)).toMatchObject(true);
expect(getBottomN(arr, 5, 6)).toMatchObject(false);
});
it('today test', () => {
const date = new Date();
expect(today(date)).toBe(true);
});
it('today test2', () => {
const date = new Date();
date.setHours(0, 0, 0, 0);
expect(today(date)).toBe(true);
});
it('tomorrow test', () => {
const date = new Date();
date.setDate(date.getDate() + 1);
expect(tomorrow(date)).toBe(true);
});
it('yesterday test', () => {
const date = new Date();
date.setDate(date.getDate() - 1);
expect(yesterday(date)).toBe(true);
});
it('thisWeek test', () => {
const date = new Date();
expect(thisWeek(date)).toBe(true);

date.setDate(date.getDate() - 7);
expect(thisWeek(date)).toBe(false);
});
});
39 changes: 33 additions & 6 deletions packages/condition/src/_tests_/test.heap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,50 @@ describe('heap test', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const small = getSmallestK(arr, 5);
const large = getLargestK(arr, 5);
expect(small).toEqual([5, 3, 4, 1, 2]); // large heap
expect(large).toEqual([6, 8, 7, 9, 10]); // small heap
expect(small.sort()).toMatchObject([5, 3, 4, 1, 2].sort()); // large heap
expect(large.sort()).toMatchObject([6, 8, 7, 10, 9].sort()); // small heap
});

it('topN test2', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const small = getSmallestK(arr, 4);
const large = getLargestK(arr, 4);
expect(small).toEqual([4]);
expect(large).toEqual([10, 9, 8, 7]);
expect(small.sort()).toMatchObject([4, 2, 3, 1].sort());
expect(large.sort()).toMatchObject([10, 9, 8, 7].sort());
});

it('topN test randomSort arr', () => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort(randomSort);
const small = getSmallestK(arr, 4);
const large = getLargestK(arr, 4);
expect(small).toEqual([4]);
expect(large).toEqual([10, 9, 8, 7]);
expect(small.sort()).toMatchObject([4, 2, 3, 1].sort());
expect(large.sort()).toMatchObject([10, 9, 8, 7].sort());
});

it('topN test randomSort arr2', () => {
const arr = [1, -2, 3, 4, -5, 6, 7, 8, 9, -10].sort(randomSort);
const small = getSmallestK(arr, 5);
const large = getLargestK(arr, 5);
expect(small.sort()).toMatchObject([3, -2, 1, -10, -5].sort());
expect(large.sort()).toMatchObject([4, 7, 6, 9, 8].sort());
});

it('topN test compare with array sort', () => {
const arr: number[] = [];
for (let i = 0; i < 100; i++) {
arr.push(Math.floor(Math.random() * 1000));
}
const compare = (a: number, b: number) => a - b;

const small = getSmallestK(arr, 5);
const large = getLargestK(arr, 5);

arr.sort(compare);
const rs = arr.slice(0, 5);
expect(rs).toMatchObject(small.sort(compare));

arr.reverse();
const rs2 = arr.slice(0, 5);
expect(rs2.sort(compare)).toMatchObject(large.sort(compare));
});
});
4 changes: 2 additions & 2 deletions packages/condition/src/condition/dynamicGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const below = (value: number, average: number): boolean => {
*/
export const getTopN = (list: number[], top: number, expectedValue: number): boolean => {
const heap = getLargestK(list, top);
return heap.include(expectedValue);
return heap.includes(expectedValue);
};

/**
Expand All @@ -57,7 +57,7 @@ export const getTopN = (list: number[], top: number, expectedValue: number): boo
*/
export const getBottomN = (list: number[], bottom: number, expectedValue: number): boolean => {
const heap = getSmallestK(list, bottom);
return heap.include(expectedValue);
return heap.includes(expectedValue);
};

/**
Expand Down
42 changes: 29 additions & 13 deletions packages/condition/src/condition/topN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@
*/

/**
* Represents a heap data structure.
* @description Represents a heap data structure.
* A heap is a complete binary tree that satisfies the heap property.
* The heap property states that the parent node is always smaller or bigger than its children.
* The root node is the smallest or biggest element in the heap.
* The heap is used to find the kth largest or smallest element in a list.
* A array can be used to represent a heap.
* The root node is at index 0, and the left child of a node at index i is at index 2i + 1.
* The right child of a node at index i is at index 2i + 2.
* The parent node of a node at index i is at index (i - 1) / 2.
* @example
* const maxHeap = [5, 3, 4, 1, 2];
* // const minHeap = [6, 8, 7, 10, 9];
* // the root node in maxHeap is 5
* // the root child is 3, 4, then the child of 3 is 1, 2
*/
class Heap {
heap: number[];
Expand Down Expand Up @@ -92,7 +105,10 @@ class Heap {
}

/**
* Represents a min heap data structure.
* @description Represents a min heap data structure.
* in MinHeap, the parent node is always smaller than its children.
* The root node is the smallest element in the heap.
* The min heap is used to find the kth largest element in a list.
*/
class MinHeap extends Heap {
/**
Expand Down Expand Up @@ -220,14 +236,14 @@ class MaxHeap extends Heap {
* @returns The kth largest element.
*/
export const getLargestK = (list: number[], k: number) => {
const min = new MinHeap();
list.forEach((n) => {
min.insert(n);
if (min.size() > k) {
min.pop();
const minHeap = new MinHeap();
for (const item of list) {
minHeap.insert(item);
if (minHeap.size() > k) {
minHeap.pop();
}
});
return min;
}
return minHeap.heap;
};

/**
Expand All @@ -238,12 +254,12 @@ export const getLargestK = (list: number[], k: number) => {
*/
export const getSmallestK = (list: number[], k: number) => {
const max = new MaxHeap();
list.forEach((n) => {
max.insert(n);
for (const item of list) {
max.insert(item);
if (max.size() > k) {
max.pop();
}
});
return max;
}
return max.heap;
};

2 changes: 2 additions & 0 deletions packages/condition/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

export * from './condition/types';
export * from './condition/funcGenerator';
export * from './condition/topN';
export * from './condition/dateGenerator';
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { CompareType, SqlFunc } from './types';
import type { CompareType, SqlFunc } from '../condition/types';

export const compareFunctionGenerator = (compareType: CompareType): SqlFunc => {
return () => {
Expand Down

0 comments on commit 17bf4c6

Please sign in to comment.