Skip to content

Commit 7c4059a

Browse files
authored
Merge pull request #117 from Exabyte-io/feature/SOF-7601
feature/SOF 7601
2 parents 96bf247 + 296b603 commit 7c4059a

22 files changed

+1392
-131
lines changed

dist/js/ArrayWithIds.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { RoundingOptions, ValueWithId } from "./ValueWithId";
2+
export declare class ArrayWithIds<T> {
3+
values: T[];
4+
ids: number[];
5+
constructor(values?: T[], ids?: number[]);
6+
static fromValues<U, C extends ArrayWithIds<U>>(this: new (values: U[], ids: number[]) => C, values: U[]): C;
7+
static fromObjects<U, C extends ArrayWithIds<U>>(this: new (values: U[], ids: number[]) => C, objects: {
8+
id: number;
9+
value: U;
10+
}[]): C;
11+
toJSON(): object[];
12+
toValueWithIdArray(): ValueWithId<T>[];
13+
getElementValueByIndex(index: number): T | undefined;
14+
getElementIdByValue(value: T): number | undefined;
15+
filterByValues(valuesToKeep: T | T[]): void;
16+
filterByIndices(indices: number | number[]): void;
17+
filterByIds(ids: number | number[], invert?: boolean): void;
18+
equals(other: ArrayWithIds<T>): boolean;
19+
mapArrayInPlace(func: (value: T) => T): void;
20+
addItem(value: T, id?: number): void;
21+
removeItem(index: number, id?: number): void;
22+
}
23+
export declare class RoundedArrayWithIds<T> extends ArrayWithIds<T> {
24+
readonly roundingOptions: RoundingOptions;
25+
constructor(values?: T[], ids?: number[], options?: RoundingOptions);
26+
toJSON(): object[];
27+
}

dist/js/ArrayWithIds.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.RoundedArrayWithIds = exports.ArrayWithIds = void 0;
4+
const ValueWithId_1 = require("./ValueWithId");
5+
class ArrayWithIds {
6+
constructor(values = [], ids = []) {
7+
if (values.length !== ids.length) {
8+
throw new Error("Values and IDs must have the same length");
9+
}
10+
this.values = [...values];
11+
this.ids = [...ids];
12+
}
13+
static fromValues(values) {
14+
const ids = values.map((_, i) => i);
15+
return new this(values, ids);
16+
}
17+
static fromObjects(objects) {
18+
const values = objects.map((obj) => obj.value);
19+
const ids = objects.map((obj) => obj.id);
20+
return new this(values, ids);
21+
}
22+
toJSON() {
23+
return this.values.map((value, index) => ({
24+
id: this.ids[index],
25+
value: value !== null &&
26+
typeof value === "object" &&
27+
"toJSON" in value &&
28+
typeof value.toJSON === "function"
29+
? value.toJSON()
30+
: value,
31+
}));
32+
}
33+
toValueWithIdArray() {
34+
return this.values.map((value, index) => ValueWithId_1.ValueWithId.fromValueAndId(value, this.ids[index]));
35+
}
36+
getElementValueByIndex(index) {
37+
return this.values[index];
38+
}
39+
getElementIdByValue(value) {
40+
const index = this.values.findIndex((v) => Array.isArray(v) && Array.isArray(value)
41+
? v.length === value.length && v.every((val, idx) => val === value[idx])
42+
: v === value);
43+
return index !== -1 ? this.ids[index] : undefined;
44+
}
45+
filterByValues(valuesToKeep) {
46+
const toHash = (v) => (Array.isArray(v) ? JSON.stringify(v) : String(v));
47+
const keepSet = new Set(Array.isArray(valuesToKeep) ? valuesToKeep.map(toHash) : [toHash(valuesToKeep)]);
48+
const filtered = this.values
49+
.map((value, i) => [value, this.ids[i]])
50+
.filter(([value]) => keepSet.has(toHash(value)));
51+
this.values = filtered.map(([v]) => v);
52+
this.ids = filtered.map(([_, id]) => id);
53+
}
54+
filterByIndices(indices) {
55+
const keepSet = new Set(Array.isArray(indices) ? indices : [indices]);
56+
this.values = this.values.filter((_, i) => keepSet.has(i));
57+
this.ids = this.ids.filter((_, i) => keepSet.has(i));
58+
}
59+
filterByIds(ids, invert = false) {
60+
const idSet = new Set(Array.isArray(ids) ? ids : [ids]);
61+
const keep = invert
62+
? this.ids.map((id, i) => (idSet.has(id) ? -1 : i)).filter((i) => i >= 0)
63+
: this.ids.map((id, i) => (idSet.has(id) ? i : -1)).filter((i) => i >= 0);
64+
this.values = keep.map((i) => this.values[i]);
65+
this.ids = keep.map((i) => this.ids[i]);
66+
}
67+
equals(other) {
68+
if (!(other instanceof ArrayWithIds))
69+
return false;
70+
if (this.values.length !== other.values.length)
71+
return false;
72+
if (this.ids.length !== other.ids.length)
73+
return false;
74+
return (this.values.every((v, i) => {
75+
const ov = other.values[i];
76+
return Array.isArray(v) && Array.isArray(ov)
77+
? v.length === ov.length && v.every((val, idx) => val === ov[idx])
78+
: v === ov;
79+
}) && this.ids.every((id, i) => id === other.ids[i]));
80+
}
81+
mapArrayInPlace(func) {
82+
this.values = this.values.map(func);
83+
}
84+
addItem(value, id) {
85+
const newId = id !== null && id !== void 0 ? id : Math.max(-1, ...this.ids) + 1;
86+
this.values.push(value);
87+
this.ids.push(newId);
88+
}
89+
removeItem(index, id) {
90+
if (id !== undefined) {
91+
index = this.ids.indexOf(id);
92+
if (index === -1)
93+
throw new Error("ID not found");
94+
}
95+
if (index < 0 || index >= this.values.length) {
96+
throw new Error("Index out of range");
97+
}
98+
this.values.splice(index, 1);
99+
this.ids.splice(index, 1);
100+
}
101+
}
102+
exports.ArrayWithIds = ArrayWithIds;
103+
class RoundedArrayWithIds extends ArrayWithIds {
104+
constructor(values = [], ids = [], options = ValueWithId_1.defaultRoundingOptions) {
105+
super(values, ids);
106+
this.roundingOptions = options;
107+
}
108+
toJSON() {
109+
return this.values.map((value, index) => new ValueWithId_1.RoundedValueWithId(this.ids[index], value, this.roundingOptions).toJSON());
110+
}
111+
}
112+
exports.RoundedArrayWithIds = RoundedArrayWithIds;

dist/js/ValueWithId.d.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ObjectWithIdAndValueSchema } from "@mat3ra/esse/dist/js/types";
2+
import { RoundingMethodEnum } from "./math";
3+
interface ValueWithIdSchema<T> {
4+
id: ObjectWithIdAndValueSchema["id"];
5+
value: T | null;
6+
}
7+
export declare class ValueWithId<T> {
8+
id: number;
9+
value: T | null;
10+
static defaultConfig: {
11+
id: number;
12+
value: null;
13+
};
14+
static fromValueAndId<U, C extends ValueWithId<U>>(this: new (args: {
15+
id: number;
16+
value: U;
17+
}) => C, value: U, id?: number): C;
18+
constructor({ id, value }?: ValueWithIdSchema<T>);
19+
/**
20+
* Converts the instance to a plain JavaScript object.
21+
*/
22+
toJSON(): object;
23+
/**
24+
* Checks if this instance is equal to another ValueWithId.
25+
*/
26+
equals<U>(other: ValueWithId<U>): boolean;
27+
}
28+
export interface RoundingOptions {
29+
precision: number;
30+
roundingMethod: RoundingMethodEnum;
31+
}
32+
export declare const defaultRoundingOptions: RoundingOptions;
33+
export declare class RoundedValueWithId<T> extends ValueWithId<T> {
34+
readonly precision: number;
35+
readonly roundingMethod: RoundingMethodEnum;
36+
constructor(id: number, value: T, options?: RoundingOptions);
37+
toJSON(): object;
38+
}
39+
export {};

dist/js/ValueWithId.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.RoundedValueWithId = exports.defaultRoundingOptions = exports.ValueWithId = void 0;
4+
const math_1 = require("./math");
5+
class ValueWithId {
6+
static fromValueAndId(value, id = 0) {
7+
return new this({ id, value });
8+
}
9+
constructor({ id, value } = ValueWithId.defaultConfig) {
10+
this.id = id;
11+
this.value = value;
12+
}
13+
/**
14+
* Converts the instance to a plain JavaScript object.
15+
*/
16+
toJSON() {
17+
if (this.value !== null &&
18+
typeof this.value === "object" &&
19+
"toJSON" in this.value &&
20+
typeof this.value.toJSON === "function") {
21+
return { id: this.id, value: this.value.toJSON() };
22+
}
23+
return { id: this.id, value: this.value };
24+
}
25+
/**
26+
* Checks if this instance is equal to another ValueWithId.
27+
*/
28+
equals(other) {
29+
if (!(other instanceof ValueWithId))
30+
return false;
31+
// because U may differ from T, we cast to unknown when comparing
32+
const v1 = this.value;
33+
const v2 = other.value;
34+
if (Array.isArray(v1) && Array.isArray(v2)) {
35+
if (v1.length !== v2.length)
36+
return false;
37+
for (let i = 0; i < v1.length; i++) {
38+
if (v1[i] !== v2[i])
39+
return false;
40+
}
41+
return this.id === other.id;
42+
}
43+
return this.id === other.id && v1 === v2;
44+
}
45+
}
46+
exports.ValueWithId = ValueWithId;
47+
ValueWithId.defaultConfig = {
48+
id: 0,
49+
value: null,
50+
};
51+
exports.defaultRoundingOptions = {
52+
precision: 9,
53+
roundingMethod: math_1.RoundingMethodEnum.HalfAwayFromZero,
54+
};
55+
class RoundedValueWithId extends ValueWithId {
56+
constructor(id, value, options = exports.defaultRoundingOptions) {
57+
super({ id, value });
58+
this.precision = options.precision;
59+
this.roundingMethod = options.roundingMethod;
60+
}
61+
toJSON() {
62+
return {
63+
id: this.id,
64+
value: math_1.math.roundArrayOrNumber(this.value, this.precision, this.roundingMethod),
65+
};
66+
}
67+
}
68+
exports.RoundedValueWithId = RoundedValueWithId;

dist/js/constants.d.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1-
export namespace coefficients {
2-
let EV_TO_RY: number;
3-
let BOHR_TO_ANGSTROM: number;
4-
let ANGSTROM_TO_BOHR: number;
5-
let EV_A_TO_RY_BOHR: number;
6-
}
7-
export namespace tolerance {
8-
let length: number;
9-
let lengthAngstrom: number;
10-
let pointsDistance: number;
11-
}
12-
export namespace units {
13-
let bohr: string;
14-
let angstrom: string;
15-
let degree: string;
16-
let radian: string;
17-
let alat: string;
18-
}
19-
export namespace ATOMIC_COORD_UNITS {
20-
let crystal: string;
21-
let cartesian: string;
22-
}
23-
export const HASH_TOLERANCE: 3;
24-
declare namespace _default {
25-
export { coefficients };
26-
export { tolerance };
27-
export { units };
28-
export { ATOMIC_COORD_UNITS };
29-
}
1+
export declare const coefficients: {
2+
EV_TO_RY: number;
3+
BOHR_TO_ANGSTROM: number;
4+
ANGSTROM_TO_BOHR: number;
5+
EV_A_TO_RY_BOHR: number;
6+
};
7+
export declare const tolerance: {
8+
length: number;
9+
lengthAngstrom: number;
10+
pointsDistance: number;
11+
};
12+
export declare const units: {
13+
bohr: string;
14+
angstrom: string;
15+
degree: string;
16+
radian: string;
17+
alat: string;
18+
};
19+
/**
20+
* @summary Coordinates units for a material's basis.
21+
*/
22+
export declare const ATOMIC_COORD_UNITS: {
23+
crystal: string;
24+
cartesian: string;
25+
};
26+
export declare const HASH_TOLERANCE = 3;
27+
declare const _default: {
28+
coefficients: {
29+
EV_TO_RY: number;
30+
BOHR_TO_ANGSTROM: number;
31+
ANGSTROM_TO_BOHR: number;
32+
EV_A_TO_RY_BOHR: number;
33+
};
34+
tolerance: {
35+
length: number;
36+
lengthAngstrom: number;
37+
pointsDistance: number;
38+
};
39+
units: {
40+
bohr: string;
41+
angstrom: string;
42+
degree: string;
43+
radian: string;
44+
alat: string;
45+
};
46+
ATOMIC_COORD_UNITS: {
47+
crystal: string;
48+
cartesian: string;
49+
};
50+
};
3051
export default _default;

dist/js/index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
import { ArrayWithIds, RoundedArrayWithIds } from "./ArrayWithIds";
12
import * as context from "./context";
23
import * as entity from "./entity";
34
import * as utils from "./utils";
4-
export declare const Code: {
5+
import { RoundedValueWithId, ValueWithId } from "./ValueWithId";
6+
import { RoundedVector3D, Vector3D } from "./vector";
7+
export { ArrayWithIds, ValueWithId, RoundedValueWithId, RoundedArrayWithIds, RoundedVector3D, Vector3D, };
8+
export { entity, context, utils };
9+
declare const Code: {
10+
ArrayWithIds: typeof ArrayWithIds;
11+
ValueWithId: typeof ValueWithId;
12+
RoundedArrayWithIds: typeof RoundedArrayWithIds;
13+
RoundedValueWithId: typeof RoundedValueWithId;
14+
RoundedVector3D: typeof RoundedVector3D;
15+
Vector3D: typeof Vector3D;
516
entity: typeof entity;
617
context: typeof context;
718
utils: typeof utils;
819
};
20+
export type CodeType = typeof Code;
21+
export default Code;

dist/js/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,31 @@ var __importStar = (this && this.__importStar) || (function () {
3333
};
3434
})();
3535
Object.defineProperty(exports, "__esModule", { value: true });
36-
exports.Code = void 0;
36+
exports.utils = exports.context = exports.entity = exports.Vector3D = exports.RoundedVector3D = exports.RoundedArrayWithIds = exports.RoundedValueWithId = exports.ValueWithId = exports.ArrayWithIds = void 0;
37+
const ArrayWithIds_1 = require("./ArrayWithIds");
38+
Object.defineProperty(exports, "ArrayWithIds", { enumerable: true, get: function () { return ArrayWithIds_1.ArrayWithIds; } });
39+
Object.defineProperty(exports, "RoundedArrayWithIds", { enumerable: true, get: function () { return ArrayWithIds_1.RoundedArrayWithIds; } });
3740
const context = __importStar(require("./context"));
41+
exports.context = context;
3842
const entity = __importStar(require("./entity"));
43+
exports.entity = entity;
3944
const utils = __importStar(require("./utils"));
40-
exports.Code = {
45+
exports.utils = utils;
46+
const ValueWithId_1 = require("./ValueWithId");
47+
Object.defineProperty(exports, "RoundedValueWithId", { enumerable: true, get: function () { return ValueWithId_1.RoundedValueWithId; } });
48+
Object.defineProperty(exports, "ValueWithId", { enumerable: true, get: function () { return ValueWithId_1.ValueWithId; } });
49+
const vector_1 = require("./vector");
50+
Object.defineProperty(exports, "RoundedVector3D", { enumerable: true, get: function () { return vector_1.RoundedVector3D; } });
51+
Object.defineProperty(exports, "Vector3D", { enumerable: true, get: function () { return vector_1.Vector3D; } });
52+
const Code = {
53+
ArrayWithIds: ArrayWithIds_1.ArrayWithIds,
54+
ValueWithId: ValueWithId_1.ValueWithId,
55+
RoundedArrayWithIds: ArrayWithIds_1.RoundedArrayWithIds,
56+
RoundedValueWithId: ValueWithId_1.RoundedValueWithId,
57+
RoundedVector3D: vector_1.RoundedVector3D,
58+
Vector3D: vector_1.Vector3D,
4159
entity,
4260
context,
4361
utils,
4462
};
63+
exports.default = Code;

0 commit comments

Comments
 (0)