native-document 1.0.68 → 1.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,97 @@
1
+ import {createFilter, createMultiSourceFilter, getSecondsOfDay, isSameDay, toDate} from "./utils";
2
+
3
+ export const dateEquals = (observableOrValue) => {
4
+ return createFilter(observableOrValue, (value, target) => {
5
+ if (!value || !target) return false;
6
+ return isSameDay(value, target);
7
+ });
8
+ };
9
+
10
+ export const dateBefore = (observableOrValue) => {
11
+ return createFilter(observableOrValue, (value, target) => {
12
+ if (!value || !target) return false;
13
+ return toDate(value) < toDate(target);
14
+ });
15
+ };
16
+
17
+ export const dateAfter = (observableOrValue) => {
18
+ return createFilter(observableOrValue, (value, target) => {
19
+ if (!value || !target) return false;
20
+ return toDate(value) > toDate(target);
21
+ });
22
+ };
23
+
24
+ export const dateBetween = (startObservableOrValue, endObservableOrValue) => {
25
+ return createMultiSourceFilter(
26
+ [startObservableOrValue, endObservableOrValue],
27
+ (value, [start, end]) => {
28
+ if (!value || !start || !end) return false;
29
+ const date = toDate(value);
30
+ return date >= toDate(start) && date <= toDate(end);
31
+ }
32
+ );
33
+ };
34
+
35
+ export const timeEquals = (observableOrValue) => {
36
+ return createFilter(observableOrValue, (value, target) => {
37
+ if (!value || !target) return false;
38
+ const d1 = toDate(value);
39
+ const d2 = toDate(target);
40
+ return d1.getHours() === d2.getHours() &&
41
+ d1.getMinutes() === d2.getMinutes() &&
42
+ d1.getSeconds() === d2.getSeconds();
43
+ });
44
+ };
45
+
46
+ export const timeAfter = (observableOrValue) => {
47
+ return createFilter(observableOrValue, (value, target) => {
48
+ if (!value || !target) return false;
49
+ return getSecondsOfDay(value) > getSecondsOfDay(target);
50
+ });
51
+ };
52
+
53
+ export const timeBefore = (observableOrValue) => {
54
+ return createFilter(observableOrValue, (value, target) => {
55
+ if (!value || !target) return false;
56
+ return getSecondsOfDay(value) < getSecondsOfDay(target);
57
+ });
58
+ };
59
+
60
+ export const timeBetween = (startObservableOrValue, endObservableOrValue) => {
61
+ return createMultiSourceFilter([startObservableOrValue, endObservableOrValue],
62
+ (value, [start, end]) => {
63
+ if (!value || !start || !end) return false;
64
+ const date = getSecondsOfDay(value);
65
+ return date >= getSecondsOfDay(start) && date <= getSecondsOfDay(end);
66
+ }
67
+ );
68
+ };
69
+
70
+ export const dateTimeEquals = (observableOrValue) => {
71
+ return createFilter(observableOrValue, (value, target) => {
72
+ if (!value || !target) return false;
73
+ return toDate(value).getTime() === toDate(target).getTime();
74
+ });
75
+ };
76
+
77
+ export const dateTimeAfter = (observableOrValue) => {
78
+ return createFilter(observableOrValue, (value, target) => {
79
+ if (!value || !target) return false;
80
+ return toDate(value) > toDate(target);
81
+ });
82
+ };
83
+
84
+ export const dateTimeBefore = (observableOrValue) => {
85
+ return createFilter(observableOrValue, (value, target) => {
86
+ if (!value || !target) return false;
87
+ return toDate(value) < toDate(target);
88
+ });
89
+ };
90
+
91
+ export const dateTimeBetween = (startObservableOrValue, endObservableOrValue) => {
92
+ return createMultiSourceFilter([startObservableOrValue, endObservableOrValue], (value, [start, end]) => {
93
+ if (!value || !start || !end) return false;
94
+ const date = toDate(value);
95
+ return date >= toDate(start) && date <= toDate(end);
96
+ });
97
+ };
@@ -0,0 +1,4 @@
1
+ export * from './utils';
2
+ export * from './standard';
3
+ export * from './date';
4
+ export * from './strings';
@@ -0,0 +1,137 @@
1
+ import Validator from "../validator";
2
+ import { createFilter, createMultiSourceFilter } from "./utils";
3
+
4
+
5
+ export function equals(observableOrValue){
6
+ return createFilter(observableOrValue, (value, target) => value === target);
7
+ }
8
+
9
+ export function notEquals(observableOrValue){
10
+ return createFilter(observableOrValue, (value, target) => value !== target);
11
+ }
12
+
13
+ export function greaterThan(observableOrValue){
14
+ return createFilter(observableOrValue, (value, target) => value > target);
15
+ }
16
+
17
+ export function greaterThanOrEqual(observableOrValue){
18
+ return createFilter(observableOrValue, (value, target) => value >= target);
19
+ }
20
+
21
+ export function lessThan(observableOrValue){
22
+ return createFilter(observableOrValue, (value, target) => value < target);
23
+ }
24
+
25
+ export function lessThanOrEqual(observableOrValue){
26
+ return createFilter(observableOrValue, (value, target) => value <= target);
27
+ }
28
+
29
+ export function between(minObservableOrValue, maxObservableOrValue){
30
+ return createMultiSourceFilter(
31
+ [minObservableOrValue, maxObservableOrValue],
32
+ (value, [min, max]) => value >= min && value <= max
33
+ );
34
+ }
35
+
36
+ export function inArray(observableOrArray){
37
+ return createFilter(observableOrArray, (value, arr) => arr.includes(value));
38
+ }
39
+
40
+ export function notIn(observableOrArray){
41
+ return createFilter(observableOrArray, (value, arr) => !arr.includes(value));
42
+ }
43
+
44
+ export function isEmpty(observableOrValue = true){
45
+ return createFilter(observableOrValue, (value, shouldBeEmpty) => {
46
+ const isActuallyEmpty = !value || value === '' ||
47
+ (Array.isArray(value) && value.length === 0);
48
+
49
+ return shouldBeEmpty ? isActuallyEmpty : !isActuallyEmpty;
50
+ });
51
+ }
52
+
53
+ export function isNotEmpty(observableOrValue = true){
54
+ return createFilter(observableOrValue, (value, shouldBeNotEmpty) => {
55
+ const isActuallyNotEmpty = !!value && value !== '' &&
56
+ (!Array.isArray(value) || value.length > 0);
57
+
58
+ return shouldBeNotEmpty ? isActuallyNotEmpty : !isActuallyNotEmpty;
59
+ });
60
+ }
61
+
62
+ export function match(patternObservableOrValue, asRegexObservableOrValue = true, flagsObservableOrValue = ''){
63
+ return createMultiSourceFilter(
64
+ [patternObservableOrValue, asRegexObservableOrValue, flagsObservableOrValue],
65
+ (value, [pattern, asRegex, flags]) => {
66
+ if (!pattern) return true;
67
+
68
+ if (asRegex){
69
+ try {
70
+ const regex = new RegExp(pattern, flags);
71
+ return regex.test(String(value));
72
+ } catch (error){
73
+ console.warn('Invalid regex pattern:', pattern, error);
74
+ return false;
75
+ }
76
+ }
77
+
78
+ if (!flags || flags === ''){
79
+ return String(value).toLowerCase().includes(String(pattern).toLowerCase());
80
+ }
81
+ return String(value).includes(String(pattern));
82
+ }
83
+ );
84
+ }
85
+
86
+ export function and(...filters){
87
+ const dependencies = filters
88
+ .flatMap(f => f.dependencies ? (Array.isArray(f.dependencies) ? f.dependencies : [f.dependencies]) : [])
89
+ .filter(Validator.isObservable);
90
+
91
+ return {
92
+ dependencies: dependencies.length > 0 ? dependencies : null,
93
+ callback: (value) => filters.every(f => f.callback(value))
94
+ };
95
+ }
96
+
97
+ export function or(...filters){
98
+ const dependencies = filters
99
+ .flatMap(f => f.dependencies ? (Array.isArray(f.dependencies) ? f.dependencies : [f.dependencies]) : [])
100
+ .filter(Validator.isObservable);
101
+
102
+ return {
103
+ dependencies: dependencies.length > 0 ? dependencies : null,
104
+ callback: (value) => filters.some(f => f.callback(value))
105
+ };
106
+ }
107
+
108
+ export function not(filter){
109
+ return {
110
+ dependencies: filter.dependencies,
111
+ callback: (value) => !filter.callback(value)
112
+ };
113
+ }
114
+
115
+ export function custom(callbackFn, ...observables){
116
+ const dependencies = observables.filter(Validator.isObservable);
117
+
118
+ return {
119
+ dependencies: dependencies.length > 0 ? dependencies : null,
120
+ callback: (value) => {
121
+ const values = observables.map(o =>
122
+ Validator.isObservable(o) ? o.val() : o
123
+ );
124
+ return callbackFn(value, ...values);
125
+ }
126
+ };
127
+ }
128
+
129
+ export const gt = greaterThan;
130
+ export const gte = greaterThanOrEqual;
131
+ export const lt = lessThan;
132
+ export const lte = lessThanOrEqual;
133
+ export const eq = equals;
134
+ export const neq = notEquals;
135
+ export const all = and;
136
+ export const any = or;
137
+
@@ -0,0 +1,35 @@
1
+ import { createFilter, createMultiSourceFilter } from "./utils";
2
+
3
+
4
+ export function includes(observableOrValue, caseSensitive = false){
5
+ return createFilter(observableOrValue, (value, query) => {
6
+ if (!value) return false;
7
+ if (!query) return true;
8
+ if (!caseSensitive){
9
+ return String(value).toLowerCase().includes(String(query).toLowerCase());
10
+ }
11
+ return String(value).includes(String(query));
12
+ });
13
+ }
14
+
15
+ export const contains = includes;
16
+
17
+ export function startsWith(observableOrValue, caseSensitive = false){
18
+ return createFilter(observableOrValue, (value, query) => {
19
+ if (!query) return true;
20
+ if (!caseSensitive){
21
+ return String(value).toLowerCase().startsWith(String(query).toLowerCase());
22
+ }
23
+ return String(value).startsWith(String(query));
24
+ });
25
+ }
26
+
27
+ export function endsWith(observableOrValue, caseSensitive = false){
28
+ return createFilter(observableOrValue, (value, query) => {
29
+ if (!query) return true;
30
+ if (!caseSensitive){
31
+ return String(value).toLowerCase().endsWith(String(query).toLowerCase());
32
+ }
33
+ return String(value).endsWith(String(query));
34
+ });
35
+ }
@@ -0,0 +1,41 @@
1
+ import Validator from "../validator";
2
+
3
+ export function toDate(value) {
4
+ if (value instanceof Date) return value;
5
+ return new Date(value);
6
+ }
7
+
8
+ export function isSameDay(date1, date2) {
9
+ const d1 = toDate(date1);
10
+ const d2 = toDate(date2);
11
+ return d1.getFullYear() === d2.getFullYear() &&
12
+ d1.getMonth() === d2.getMonth() &&
13
+ d1.getDate() === d2.getDate();
14
+ }
15
+
16
+ export function getSecondsOfDay(date) {
17
+ const d = toDate(date);
18
+ return (d.getHours() * 3600) + (d.getMinutes() * 60) + d.getSeconds();
19
+ }
20
+
21
+ export function createFilter(observableOrValue, callbackFn){
22
+ const isObservable = Validator.isObservable(observableOrValue);
23
+
24
+ return {
25
+ dependencies: isObservable ? observableOrValue : null,
26
+ callback: (value) => callbackFn(value, isObservable ? observableOrValue.val() : observableOrValue)
27
+ };
28
+ }
29
+
30
+ export function createMultiSourceFilter(sources, callbackFn){
31
+ const observables = sources.filter(Validator.isObservable);
32
+
33
+ const getValues = () => sources.map(src =>
34
+ Validator.isObservable(src) ? src.val() : src
35
+ );
36
+
37
+ return {
38
+ dependencies: observables.length > 0 ? observables : null,
39
+ callback: (value) => callbackFn(value, getValues())
40
+ };
41
+ }
File without changes
File without changes
@@ -0,0 +1,43 @@
1
+ import { ObservableItem } from '../observable';
2
+ import { FilterResult } from './types';
3
+
4
+ export type DateValue = Date | string | number | ObservableItem<Date | string | number>;
5
+
6
+ export function toDate(value: Date | string | number): Date;
7
+
8
+ export function isSameDay(date1: Date | string | number, date2: Date | string | number): boolean;
9
+
10
+ export function getSecondsOfDay(date: Date | string | number): number;
11
+
12
+ export function dateEquals(observableOrValue: DateValue): FilterResult<DateValue>;
13
+
14
+ export function dateBefore(observableOrValue: DateValue): FilterResult<DateValue>;
15
+
16
+ export function dateAfter(observableOrValue: DateValue): FilterResult<DateValue>;
17
+
18
+ export function dateBetween(
19
+ startObservableOrValue: DateValue,
20
+ endObservableOrValue: DateValue
21
+ ): FilterResult<DateValue>;
22
+
23
+ export function timeEquals(observableOrValue: DateValue): FilterResult<DateValue>;
24
+
25
+ export function timeAfter(observableOrValue: DateValue): FilterResult<DateValue>;
26
+
27
+ export function timeBefore(observableOrValue: DateValue): FilterResult<DateValue>;
28
+
29
+ export function timeBetween(
30
+ startObservableOrValue: DateValue,
31
+ endObservableOrValue: DateValue
32
+ ): FilterResult<DateValue>;
33
+
34
+ export function dateTimeEquals(observableOrValue: DateValue): FilterResult<DateValue>;
35
+
36
+ export function dateTimeAfter(observableOrValue: DateValue): FilterResult<DateValue>;
37
+
38
+ export function dateTimeBefore(observableOrValue: DateValue): FilterResult<DateValue>;
39
+
40
+ export function dateTimeBetween(
41
+ startObservableOrValue: DateValue,
42
+ endObservableOrValue: DateValue
43
+ ): FilterResult<DateValue>;
@@ -0,0 +1,71 @@
1
+ import { ObservableOrValue, FilterResult } from './types';
2
+ import {ObservableItem} from "../observable";
3
+
4
+
5
+ export function equals<T>(
6
+ observableOrValue: ObservableOrValue<T>
7
+ ): FilterResult<T>;
8
+
9
+ export function notEquals<T>(
10
+ observableOrValue: ObservableOrValue<T>
11
+ ): FilterResult<T>;
12
+
13
+ export function greaterThan<T extends number | Date>(
14
+ observableOrValue: ObservableOrValue<T>
15
+ ): FilterResult<T>;
16
+
17
+ export function greaterThanOrEqual<T extends number | Date>(
18
+ observableOrValue: ObservableOrValue<T>
19
+ ): FilterResult<T>;
20
+
21
+ export function lessThan<T extends number | Date>(
22
+ observableOrValue: ObservableOrValue<T>
23
+ ): FilterResult<T>;
24
+
25
+ export function lessThanOrEqual<T extends number | Date>(
26
+ observableOrValue: ObservableOrValue<T>
27
+ ): FilterResult<T>;
28
+
29
+ export function between<T extends number | Date>(
30
+ minObservableOrValue: ObservableOrValue<T>,
31
+ maxObservableOrValue: ObservableOrValue<T>
32
+ ): FilterResult<T>;
33
+
34
+ export function inArray<T>(
35
+ observableOrArray: ObservableOrValue<T[]>
36
+ ): FilterResult<T>;
37
+
38
+ export function notIn<T>(
39
+ observableOrArray: ObservableOrValue<T[]>
40
+ ): FilterResult<T>;
41
+
42
+ export function isEmpty(
43
+ observableOrValue?: ObservableOrValue<boolean>
44
+ ): FilterResult<ObservableOrValue<boolean>>;
45
+
46
+ export function isNotEmpty(
47
+ observableOrValue?: ObservableOrValue<boolean>
48
+ ): FilterResult<ObservableOrValue<boolean>>;
49
+
50
+ export function match(
51
+ patternObservableOrValue: ObservableOrValue<string | RegExp>,
52
+ asRegexObservableOrValue?: ObservableOrValue<boolean>,
53
+ flagsObservableOrValue?: ObservableOrValue<string>
54
+ ): FilterResult<string | RegExp>;
55
+
56
+ export function and<T>(...filters: FilterResult<T>[]): FilterResult<T>;
57
+
58
+ export function or<T>(...filters: FilterResult<T>[]): FilterResult<T>;
59
+
60
+ export function not<T>(filter: FilterResult<T>): FilterResult<T>;
61
+
62
+
63
+
64
+ export const gt: typeof greaterThan;
65
+ export const gte: typeof greaterThanOrEqual;
66
+ export const lt: typeof lessThan;
67
+ export const lte: typeof lessThanOrEqual;
68
+ export const eq: typeof equals;
69
+ export const neq: typeof notEquals;
70
+ export const all: typeof and;
71
+ export const any: typeof or;
@@ -0,0 +1,21 @@
1
+ import {FilterResult, ObservableOrValue} from './types';
2
+
3
+ export function contains(
4
+ observableOrValue: ObservableOrValue<string>,
5
+ caseSensitive?: boolean
6
+ ): FilterResult<string>;
7
+
8
+ export function includes(
9
+ observableOrValue: ObservableOrValue<string>,
10
+ caseSensitive?: boolean
11
+ ): FilterResult<string>;
12
+
13
+ export function startsWith(
14
+ observableOrValue: ObservableOrValue<string>,
15
+ caseSensitive?: boolean
16
+ ): FilterResult<string>;
17
+
18
+ export function endsWith(
19
+ observableOrValue: ObservableOrValue<string>,
20
+ caseSensitive?: boolean
21
+ ): FilterResult<string>;
@@ -0,0 +1,20 @@
1
+ import { ObservableItem } from '../observable';
2
+
3
+ export interface FilterResult<T = any> {
4
+ dependencies: ObservableItem | ObservableItem[] | null;
5
+ callback: (value: T) => boolean;
6
+ }
7
+
8
+ export type ObservableOrValue<T> = T | ObservableItem<T>;
9
+
10
+ export type Predicate<T = any> =
11
+ | T
12
+ | ((value: T) => boolean)
13
+ | FilterResult<T>
14
+ | ObservableItem<T>;
15
+
16
+ export type PredicateMap<T> = {
17
+ [K in keyof T]?: Predicate<T[K]>;
18
+ } & {
19
+ _?: FilterResult<T>;
20
+ };
File without changes
File without changes
@@ -1,3 +1,5 @@
1
+ import {FilterResult, PredicateMap} from "./filters/types";
2
+
1
3
  export type Unsubscribe = () => void;
2
4
 
3
5
  // Observable system type definitions
@@ -89,6 +91,10 @@ export interface ObservableArray<T> extends ObservableItem<T[]> {
89
91
  at(index: number): T | undefined;
90
92
  findIndex(callback: (value: T, index: number, array: T[]) => boolean): number;
91
93
  concat(...items: (T | T[])[]): T[];
94
+
95
+ where(predicates: PredicateMap<T>): ObservableArray<T>;
96
+ whereSome<K extends keyof T>(fields: K[], filter: FilterResult<T[K]>): ObservableArray<T>;
97
+ whereEvery<K extends keyof T>(fields: K[], filter: FilterResult<T[K]>): ObservableArray<T>;
92
98
  }
93
99
 
94
100
  export type ObservableProxy<T extends Record<string, any>> = {
@@ -134,7 +140,7 @@ export interface ObservableConfig {
134
140
 
135
141
  export interface ObservableStatic {
136
142
  <T>(value: T, configs?: ObservableConfig | null): ObservableItem<T>;
137
- array<T>(target: T[], configs?: ObservableConfig | null): ObservableArray<T>;
143
+ array<T>(target: T[] | null, configs?: ObservableConfig | null): ObservableArray<T>;
138
144
 
139
145
  init<T extends Record<string, any>>(value: T, configs?: ObservableConfig | null): ObservableProxy<T>;
140
146
  object<T extends Record<string, any>>(value: T, configs?: ObservableConfig | null): ObservableProxy<T>;
File without changes
package/utils.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import NativeFetch from "./src/utils/fetch/NativeFetch";
2
2
  import { Service } from "./src/utils/service";
3
+ import * as Filters from "./src/utils/filters/index";
3
4
 
4
5
 
5
6
  export {
6
7
  NativeFetch,
7
- Service
8
+ Service,
9
+ Filters,
8
10
  };