@arcote.tech/arc 0.0.12 → 0.0.13

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.
@@ -15,7 +15,8 @@ declare class ArcCollection<Name extends string, Id extends ArcIdAny, Schema ext
15
15
  readonly name: Name;
16
16
  readonly id: Id;
17
17
  readonly schema: Schema;
18
- constructor(name: Name, id: Id, schema: Schema);
18
+ readonly options: ArcCollectionOptions<Id, Schema>;
19
+ constructor(name: Name, id: Id, schema: Schema, options: ArcCollectionOptions<Id, Schema>);
19
20
  serialize(data: objectUtil.simplify<{
20
21
  _id: util.FirstArgument<Id["serialize"]>;
21
22
  } & objectUtil.addQuestionMarks<util.FirstArgument<Schema["serialize"]>>>): objectUtil.simplify<{
@@ -45,7 +46,7 @@ export declare class ArcIndexedCollection<Name extends string, Id extends ArcIdA
45
46
  [name: string]: Indexes[];
46
47
  }> extends ArcCollection<Name, Id, Schema> implements ArcContextElement {
47
48
  readonly indexes: I;
48
- constructor(name: Name, id: Id, schema: Schema, indexes: I);
49
+ constructor(name: Name, id: Id, schema: Schema, options: ArcCollectionOptions<Id, Schema>, indexes: I);
49
50
  queryBuilder(): {
50
51
  all: () => ArcAllItemsQueryBuilder<ArcCollection<Name, Id, Schema>>;
51
52
  one: (id: util.GetType<Id> | undefined) => ArcOneItemQueryBuilder<ArcCollection<Name, Id, Schema>>;
@@ -57,5 +58,8 @@ export declare class ArcIndexedCollection<Name extends string, Id extends ArcIdA
57
58
  }
58
59
  export type ArcCollectionAny = ArcCollection<any, any, any>;
59
60
  export type ArcIndexedCollectionAny = ArcIndexedCollection<any, any, any, any, any>;
60
- export declare function collection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny>(name: Name, id: Id, schema: Schema): ArcCollection<Name, Id, Schema>;
61
+ type ArcCollectionOptions<Id extends ArcIdAny, Schema extends ArcObjectAny> = {
62
+ sort?: (a: Deserialize<Id, Schema>, b: Deserialize<Id, Schema>) => number;
63
+ };
64
+ export declare function collection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny>(name: Name, id: Id, schema: Schema, options?: ArcCollectionOptions<Id, Schema>): ArcCollection<Name, Id, Schema>;
61
65
  export {};
@@ -1,9 +1,11 @@
1
1
  import { ArcQueryBuilder } from "../../context/query-builders";
2
2
  import type { util } from "../../utils";
3
3
  import { type ArcCollectionAny } from "../collection";
4
+ type SortComparator<C extends ArcCollectionAny> = ((a: util.CollectionItemWithId<C>, b: util.CollectionItemWithId<C>) => number) | Array<[keyof util.CollectionItemWithId<C>, "ASC" | "DESC"]>;
4
5
  export declare abstract class ArcManyItemsQueryBuilder<C extends ArcCollectionAny> extends ArcQueryBuilder {
5
6
  protected filterFn?: (item: util.CollectionItemWithId<C>) => boolean;
6
7
  protected sortFn?: (a: util.CollectionItemWithId<C>, b: util.CollectionItemWithId<C>) => number;
7
8
  filter(callback: (item: util.CollectionItemWithId<C>) => boolean): this;
8
- sort(callback: (a: util.CollectionItemWithId<C>, b: util.CollectionItemWithId<C>) => number): this;
9
+ sort(comparator: SortComparator<C>): this;
9
10
  }
11
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { ArcElement } from "./element";
2
+ import type { ArcRawShape } from "./object";
3
+ import { ArcOptional } from "./optional";
4
+ export declare function ArcClass<E extends ArcRawShape>(shape: E): {
5
+ new (): { [key in keyof E]: ReturnType<E[key]["deserialize"]>; };
6
+ };
7
+ export declare class ArcInstanceOf<E> implements ArcElement {
8
+ private Class;
9
+ constructor(Class: {
10
+ new (): E;
11
+ });
12
+ serialize(value: unknown): unknown;
13
+ deserialize(value: unknown): E;
14
+ parse(value: unknown): unknown;
15
+ optional(): ArcOptional<this>;
16
+ }
17
+ export declare function instanceOf<E>(element: {
18
+ new (): E;
19
+ }): ArcInstanceOf<E>;
@@ -0,0 +1,11 @@
1
+ import type { util } from "../utils";
2
+ import type { ArcElement } from "./element";
3
+ export declare class ArcOr<T extends ArcElement[]> implements ArcElement {
4
+ private elements;
5
+ constructor(elements: T);
6
+ serialize(value: util.FirstArgument<T[number]["serialize"]>): ReturnType<T[number]["serialize"]>;
7
+ deserialize(value: util.FirstArgument<T[number]["deserialize"]>): ReturnType<T[number]["deserialize"]>;
8
+ parse(value: util.FirstArgument<T[number]["parse"]>): ReturnType<T[number]["parse"]>;
9
+ private isTypeOf;
10
+ }
11
+ export declare function or<T extends ArcElement[]>(...elements: T): ArcOr<T>;
@@ -0,0 +1,20 @@
1
+ import { type util } from "../utils";
2
+ import { ArcAbstract } from "./abstract";
3
+ import type { ArcElement } from "./element";
4
+ import { ArcOptional } from "./optional";
5
+ export declare class ArcRecurent<E extends ArcElement, Brand extends string | symbol> implements ArcElement {
6
+ private parent;
7
+ constructor(parent: E);
8
+ serialize(value: util.FirstArgument<E["serialize"]> & {
9
+ __brand: Brand;
10
+ }): ReturnType<E["serialize"]>;
11
+ deserialize(value: util.FirstArgument<E["deserialize"]>): ReturnType<E["deserialize"]> & {
12
+ __brand: Brand;
13
+ };
14
+ parse(value: util.FirstArgument<E["parse"]>): ReturnType<E["parse"]> & {
15
+ __brand: Brand;
16
+ };
17
+ optional(): ArcOptional<this>;
18
+ }
19
+ export type ArcRecurentAny = ArcRecurent<ArcAbstract, any>;
20
+ export declare function recurent<E extends () => ArcElement>(element: E): ArcRecurent<ArcElement, string | symbol>;
package/dist/index.js CHANGED
@@ -114,6 +114,23 @@ class ArcQueryBuilder {
114
114
  }
115
115
 
116
116
  // collection/query-builders/abstract-many-items.ts
117
+ function sortComparatorBuilder(comparator) {
118
+ if (typeof comparator === "function") {
119
+ return comparator;
120
+ } else if (Array.isArray(comparator)) {
121
+ return (a, b) => {
122
+ for (const [key, order] of comparator) {
123
+ if (a[key] < b[key])
124
+ return order === "ASC" ? -1 : 1;
125
+ if (a[key] > b[key])
126
+ return order === "ASC" ? 1 : -1;
127
+ }
128
+ return 0;
129
+ };
130
+ }
131
+ throw new Error("Invalid comparator type");
132
+ }
133
+
117
134
  class ArcManyItemsQueryBuilder extends ArcQueryBuilder {
118
135
  filterFn;
119
136
  sortFn;
@@ -121,8 +138,8 @@ class ArcManyItemsQueryBuilder extends ArcQueryBuilder {
121
138
  this.filterFn = callback;
122
139
  return this;
123
140
  }
124
- sort(callback) {
125
- this.sortFn = callback;
141
+ sort(comparator) {
142
+ this.sortFn = sortComparatorBuilder(comparator);
126
143
  return this;
127
144
  }
128
145
  }
@@ -133,6 +150,8 @@ class ArcAllItemsQueryBuilder extends ArcManyItemsQueryBuilder {
133
150
  constructor(collection) {
134
151
  super();
135
152
  this.collection = collection;
153
+ if (collection.options.sort)
154
+ this.sortFn = collection.options.sort;
136
155
  }
137
156
  toQuery() {
138
157
  return new ArcAllItemsQuery(this.collection, this.filterFn, this.sortFn);
@@ -175,6 +194,8 @@ class ArcIndexedItemsQueryBuilder extends ArcManyItemsQueryBuilder {
175
194
  this.collection = collection;
176
195
  this.index = index;
177
196
  this.data = data;
197
+ if (collection.options.sort)
198
+ this.sortFn = collection.options.sort;
178
199
  }
179
200
  toQuery() {
180
201
  return new ArcIndexedItemsQuery(this.collection, this.index, this.data, this.filterFn, this.sortFn);
@@ -217,18 +238,20 @@ class ArcOneItemQueryBuilder extends ArcQueryBuilder {
217
238
  }
218
239
 
219
240
  // collection/collection.ts
220
- function collection(name, id, schema) {
221
- return new ArcCollection(name, id, schema);
241
+ function collection(name, id, schema, options = {}) {
242
+ return new ArcCollection(name, id, schema, options);
222
243
  }
223
244
 
224
245
  class ArcCollection {
225
246
  name;
226
247
  id;
227
248
  schema;
228
- constructor(name, id, schema) {
249
+ options;
250
+ constructor(name, id, schema, options) {
229
251
  this.name = name;
230
252
  this.id = id;
231
253
  this.schema = schema;
254
+ this.options = options;
232
255
  }
233
256
  serialize(data) {
234
257
  return {
@@ -304,14 +327,14 @@ class ArcCollection {
304
327
  }
305
328
  }
306
329
  indexBy(indexes) {
307
- return new ArcIndexedCollection(this.name, this.id, this.schema, indexes);
330
+ return new ArcIndexedCollection(this.name, this.id, this.schema, this.options, indexes);
308
331
  }
309
332
  }
310
333
 
311
334
  class ArcIndexedCollection extends ArcCollection {
312
335
  indexes;
313
- constructor(name, id, schema, indexes) {
314
- super(name, id, schema);
336
+ constructor(name, id, schema, options, indexes) {
337
+ super(name, id, schema, options);
315
338
  this.indexes = indexes;
316
339
  }
317
340
  queryBuilder() {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "version": "0.0.12",
7
+ "version": "0.0.13",
8
8
  "private": false,
9
9
  "author": "Przemysław Krasiński [arcote.tech]",
10
10
  "description": "Arc is a framework designed to align code closely with business logic, streamlining development and enhancing productivity.",