Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat condition vickun #3448

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions packages/condition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# @univerjs/univer-condition

[![npm version](https://img.shields.io/npm/v/@univerjs/univer-condition)](https://npmjs.org/packages/@univerjs/univer-condition)
[![license](https://img.shields.io/npm/l/@univerjs/univer-condition)](https://img.shields.io/npm/l/@univerjs/univer-condition)

## Introduction

> In sheet, there are many features have ability to custom a rule , when the rule is match, something will happen.

- A formatter will be used, (condition format)
- A hidden row action will execute, (filter)
- A validation will be apply (data validation)
- A pivot filter will be execute (pivot table filter)

### design

``` typescript
interface ICondition {
conType :ConType;
compareType:compareType
expect:any;
}

enum ConType{

}
enum compareType{

}
```

## Usage

### Installation

```shell
# Using npm
npm i @univerjs/univer-condition

# Using pnpm
pnpm add @univerjs/univer-condition
```

### Classification

#### Number

> For compare type operator , the following table list all

| enum | description |
| :----------------- | :---------------------------------------------------------------------------------------------------------------------------- |
| equal | The given value is equal to the expected |
| notEqual | a value is not equal to a expected value |
| greaterThan | a value is greater than to a expected value |
| greaterThanOrEqual | a value is greater than or equal to a expected value |
| lessThan | a value is less than a expected value |
| lessThanOrEqual | a value is less than than or equal to a expected value |
| between | closed interval , a value is greater than or equal to the smaller expected value, and less than or equal to the bigger value |
| notBetween | open interval, a value is greater than or equal to the bigger expected value, and less than or equal to the smaller value |

> The following show statistics operator

| enum | description |
| :---- | :----------------------------------------- |
| above | the value is bigger than the average value |
| below | the value is less than the average value |
| top10 | Value in the top/bottom N values |



#### Date
> The date is a special type in excel, but in fact ,there are no date value in excel , the date be expressed by a number value and a format

For date type condition , there are there kinds: Date dynamic condition , Date Compare condition, Date Choose Condition, Date group

- when using whole day is set, it means the data 2024/05/27 00-00-01 is equal to the data 2024/05/27 23-59-59

##### Date dynamic condition

- Notice : In most country , the Saturday is last day of a week

| enum | description |
| :---------- | :------------------------------------------- |
| tomorrow | day after today |
| today | today |
| yesterday | yesterday |
| nextWeek | next week |
| thisWeek | this week |
| lastWeek | last week |
| nextMonth | next month |
| thisMonth | this month |
| lastMonth | last month |
| nextQuarter | next quarter |
| thisQuarter | this quarter |
| lastQuarter | last quarter |
| nextYear | next year |
| thisYear | this year |
| lastYear | last year |
| yearToDate | the time area for this year begin to current |

##### Date Compare condition



| enum | description |
| :------------------- | :---------------------------------------- |
| dateEqual | the same date |
| dateNotEqual | not the same date |
| dateOlderThan | old than expected date |
| dateOlderThanOrEqual | old than or equal date |
| dateNewerThan | before expected date |
| dateNewerThanOrEqual | before or equal expected date |
| dateBetween | a date is between two other expected date |
| dateNotBetween | a date is between two other expected date |

##### Date Choose Condition

| enum | description |
| :--- | :--------------------------- |
| Q1 | The first quarter of a year |
| Q2 | The second quarter of a year |
| Q3 | The 3'th quarter of a year |
| Q4 | The last quarter of a year |
| M1 | January |
| M2 | February |
| M3 | March |
| M4 | April |
| M5 | May |
| M6 | June |
| M7 | July |
| M8 | August |
| M9 | September |
| M10 | October |
| M11 | November |
| M12 | December |

#### Date group
| enum | description |
| :----- | :-------------- |
| day | Group by day |
| hour | Group by hour |
| minute | Group by minute |
| month | Group by month |
| second | Group by second |
| year | Group by year |



#### Text

| enum | description |
| :----------------- | :---------------------------------------------------------------------------------------------------------------------------- |
| Equal | a value is equal a expected value |
| NotEqual | a value is not equal a expected value |
| BeginsWith | the string start with expected text |
| NotBeginsWith | the string not start with expected text |
| EndsWith | the string end with expected text |
| NotEndsWith | the string not end with expected text |
| Contains | the string contain expected text |
| NotContains | the string not contain expected text |
| GreaterThan | a value is greater than the expected value |
| GreaterThanOrEqual | a value is greater than or equal to the expected value |
| LessThan | a value is greater than the expected value |
| LessThanOrEqual | a value is greater than or equal the expected value |
| Between | closed interval , a value is greater than or equal to the smaller expected value, and less than or equal to the bigger value |
| NotBetween | open interval, a value is greater than or equal to the bigger expected value, and less than or equal to the smaller value |

#### Logic
> A logic condition can combine some condition together

| enum | description |
| :--- | :------------------------------------------------------------------------------ |
| not | return a opposite of expect condition |
| and | all expect conditions is true it will return true , otherwise it will be false |
| or | one of expect conditions is true , it will return true |

#### Color & IconSet

- For color , there are font color and cell color two kinds of a cell
- For icon set filter, this element specifies the icon set and particular icon within that set to filter by. For any cells whose icon does
not match the specified criteria, the corresponding rows shall be hidden from view when the filter is applied.

75 changes: 75 additions & 0 deletions packages/condition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "@univerjs/univer-condition",
"version": "0.0.1",
"private": true,
"description": "",
"author": "DreamNum <[email protected]>",
"license": "Apache-2.0",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/univer"
},
"homepage": "https://univer.ai",
"repository": {
"type": "git",
"url": "https://github.com/dream-num/univer"
},
"bugs": {
"url": "https://github.com/dream-num/univer/issues"
},
"keywords": [],
"exports": {
".": "./src/index.ts",
"./*": "./src/*"
},
"main": "./lib/cjs/index.js",
"module": "./lib/es/index.js",
"types": "./lib/types/index.d.ts",
"publishConfig": {
"access": "public",
"main": "./lib/cjs/index.js",
"module": "./lib/es/index.js",
"exports": {
".": {
"import": "./lib/es/index.js",
"require": "./lib/cjs/index.js",
"types": "./lib/types/index.d.ts"
},
"./*": {
"import": "./lib/es/*",
"require": "./lib/cjs/*",
"types": "./lib/types/index.d.ts"
},
"./lib/*": "./lib/*"
}
},
"directories": {
"lib": "lib"
},
"files": [
"lib"
],
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"lint:types": "tsc --noEmit",
"build": "tsc && vite build"
},
"peerDependencies": {
"@univerjs/core": "workspace:*",
"@wendellhu/redi": ">=0.12.13",
"rxjs": ">=7.0.0"
},
"dependencies": {
},
"devDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/shared": "workspace:*",
"@wendellhu/redi": "^0.15.2",
"rxjs": "^7.8.1",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"vitest": "^1.6.0"
}
}
117 changes: 117 additions & 0 deletions packages/condition/src/_tests_/test.date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 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 { dateM1, dateM10, dateM11, dateM12, dateM2, dateM3, dateM4, dateM5, dateM6, dateM7, dateM8, dateM9, dateQ1, dateQ2, dateQ3, dateQ4 } from '../condition/dateGenerator';

describe('date grouping test', () => {
it('dateM1 test', () => {
const date = new Date();
date.setMonth(0, 1);
expect(dateM1(date)).toBe(true);
});
it('dateM2 test', () => {
const date = new Date();
date.setMonth(1, 1);
expect(dateM2(date)).toBe(true);
});
it('dateM3 test', () => {
const date = new Date();
date.setMonth(2, 1);
expect(dateM3(date)).toBe(true);
});
it('dateM4 test', () => {
const date = new Date();
date.setMonth(3, 1);
expect(dateM4(date)).toBe(true);
});
it('dateM5 test', () => {
const date = new Date();
date.setMonth(4, 1);
expect(dateM5(date)).toBe(true);
});
it('dateM6 test', () => {
const date = new Date();
date.setMonth(5, 1);
expect(dateM6(date)).toBe(true);
});
it('dateM7 test', () => {
const date = new Date();
date.setMonth(6, 1);
expect(dateM7(date)).toBe(true);
});
it('dateM8 test', () => {
const date = new Date();
date.setMonth(7, 1);
expect(dateM8(date)).toBe(true);
});
it('dateM9 test', () => {
const date = new Date();
date.setMonth(8, 1);
expect(dateM9(date)).toBe(true);
});
it('dateM10 test', () => {
const date = new Date();
date.setMonth(9, 1);
expect(dateM10(date)).toBe(true);
});
it('dateM11 test', () => {
const date = new Date();
date.setMonth(10, 1);
expect(dateM11(date)).toBe(true);
});
it('dateM12 test', () => {
const date = new Date();
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);
});
});
Loading
Loading