Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ec0f4d6
feat: add array and value with ids (claude)
VsevolodX Apr 14, 2025
84bdf4e
update: adjust value with id + add tests
VsevolodX Apr 14, 2025
abcc843
chore: value, id order
VsevolodX Apr 14, 2025
8847c2f
update: adjust array with ids
VsevolodX Apr 15, 2025
6d2ca01
update: add tests for array with ids
VsevolodX Apr 15, 2025
f77412b
update: add rounded class
VsevolodX Apr 15, 2025
4d5ab39
update: add rounded array class
VsevolodX Apr 15, 2025
4006319
chore: revert order
VsevolodX Apr 15, 2025
0ac5519
update: add rounding to math
VsevolodX Apr 15, 2025
82e5099
update: add tests for rounding:
VsevolodX Apr 15, 2025
4bb90d2
chore: name consts humanly
VsevolodX Apr 16, 2025
9925fff
chore: name consts humanly 2
VsevolodX Apr 16, 2025
3b6957f
update: fix mistake caught by tests
VsevolodX Apr 16, 2025
fd6d1b7
chore: name consts humanly 3
VsevolodX Apr 16, 2025
a54cf3e
chore: ubuntu to 24.04
VsevolodX Apr 16, 2025
f0d2271
chore: generate dist
VsevolodX Apr 17, 2025
1b60b7c
chore: export classes
VsevolodX Apr 17, 2025
d85021a
chore: export classes
VsevolodX Apr 17, 2025
0201b29
try: adjsut export
VsevolodX Apr 17, 2025
89cb259
try: reexport
VsevolodX Apr 18, 2025
f7565e6
update: rounded classes
VsevolodX Apr 18, 2025
f28c505
feat: add vector and roudned vector
VsevolodX Apr 18, 2025
865c530
update: add tests for vector
VsevolodX Apr 18, 2025
77e4cdb
update: add defensive assignment
VsevolodX Apr 18, 2025
1a5117c
update: add export
VsevolodX Apr 18, 2025
0ca1b30
chore: main export
VsevolodX Apr 19, 2025
27b5896
wip: export math
VsevolodX Apr 19, 2025
88e9d2c
update: fix generic
VsevolodX Apr 19, 2025
762ea8c
update: vector
VsevolodX Apr 20, 2025
82e0c62
update: adjust valuwithid
VsevolodX Apr 20, 2025
324e8ff
update: fix equlaity:
VsevolodX Apr 20, 2025
bf514e4
update: fix from value and id
VsevolodX Apr 20, 2025
c68df7f
update: mathjs types
VsevolodX Apr 22, 2025
4b90f08
chore: correct import
VsevolodX Apr 22, 2025
f9e974a
chore: esse
VsevolodX Apr 22, 2025
296b603
Merge branch 'main' into feature/SOF-7601
VsevolodX Apr 22, 2025
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
27 changes: 27 additions & 0 deletions dist/js/ArrayWithIds.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RoundingOptions, ValueWithId } from "./ValueWithId";
export declare class ArrayWithIds<T> {
values: T[];
ids: number[];
constructor(values?: T[], ids?: number[]);
static fromValues<U, C extends ArrayWithIds<U>>(this: new (values: U[], ids: number[]) => C, values: U[]): C;
static fromObjects<U, C extends ArrayWithIds<U>>(this: new (values: U[], ids: number[]) => C, objects: {
id: number;
value: U;
}[]): C;
toJSON(): object[];
toValueWithIdArray(): ValueWithId<T>[];
getElementValueByIndex(index: number): T | undefined;
getElementIdByValue(value: T): number | undefined;
filterByValues(valuesToKeep: T | T[]): void;
filterByIndices(indices: number | number[]): void;
filterByIds(ids: number | number[], invert?: boolean): void;
equals(other: ArrayWithIds<T>): boolean;
mapArrayInPlace(func: (value: T) => T): void;
addItem(value: T, id?: number): void;
removeItem(index: number, id?: number): void;
}
export declare class RoundedArrayWithIds<T> extends ArrayWithIds<T> {
readonly roundingOptions: RoundingOptions;
constructor(values?: T[], ids?: number[], options?: RoundingOptions);
toJSON(): object[];
}
112 changes: 112 additions & 0 deletions dist/js/ArrayWithIds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoundedArrayWithIds = exports.ArrayWithIds = void 0;
const ValueWithId_1 = require("./ValueWithId");
class ArrayWithIds {
constructor(values = [], ids = []) {
if (values.length !== ids.length) {
throw new Error("Values and IDs must have the same length");
}
this.values = [...values];
this.ids = [...ids];
}
static fromValues(values) {
const ids = values.map((_, i) => i);
return new this(values, ids);
}
static fromObjects(objects) {
const values = objects.map((obj) => obj.value);
const ids = objects.map((obj) => obj.id);
return new this(values, ids);
}
toJSON() {
return this.values.map((value, index) => ({
id: this.ids[index],
value: value !== null &&
typeof value === "object" &&
"toJSON" in value &&
typeof value.toJSON === "function"
? value.toJSON()
: value,
}));
}
toValueWithIdArray() {
return this.values.map((value, index) => ValueWithId_1.ValueWithId.fromValueAndId(value, this.ids[index]));
}
getElementValueByIndex(index) {
return this.values[index];
}
getElementIdByValue(value) {
const index = this.values.findIndex((v) => Array.isArray(v) && Array.isArray(value)
? v.length === value.length && v.every((val, idx) => val === value[idx])
: v === value);
return index !== -1 ? this.ids[index] : undefined;
}
filterByValues(valuesToKeep) {
const toHash = (v) => (Array.isArray(v) ? JSON.stringify(v) : String(v));
const keepSet = new Set(Array.isArray(valuesToKeep) ? valuesToKeep.map(toHash) : [toHash(valuesToKeep)]);
const filtered = this.values
.map((value, i) => [value, this.ids[i]])
.filter(([value]) => keepSet.has(toHash(value)));
this.values = filtered.map(([v]) => v);
this.ids = filtered.map(([_, id]) => id);
}
filterByIndices(indices) {
const keepSet = new Set(Array.isArray(indices) ? indices : [indices]);
this.values = this.values.filter((_, i) => keepSet.has(i));
this.ids = this.ids.filter((_, i) => keepSet.has(i));
}
filterByIds(ids, invert = false) {
const idSet = new Set(Array.isArray(ids) ? ids : [ids]);
const keep = invert
? this.ids.map((id, i) => (idSet.has(id) ? -1 : i)).filter((i) => i >= 0)
: this.ids.map((id, i) => (idSet.has(id) ? i : -1)).filter((i) => i >= 0);
this.values = keep.map((i) => this.values[i]);
this.ids = keep.map((i) => this.ids[i]);
}
equals(other) {
if (!(other instanceof ArrayWithIds))
return false;
if (this.values.length !== other.values.length)
return false;
if (this.ids.length !== other.ids.length)
return false;
return (this.values.every((v, i) => {
const ov = other.values[i];
return Array.isArray(v) && Array.isArray(ov)
? v.length === ov.length && v.every((val, idx) => val === ov[idx])
: v === ov;
}) && this.ids.every((id, i) => id === other.ids[i]));
}
mapArrayInPlace(func) {
this.values = this.values.map(func);
}
addItem(value, id) {
const newId = id !== null && id !== void 0 ? id : Math.max(-1, ...this.ids) + 1;
this.values.push(value);
this.ids.push(newId);
}
removeItem(index, id) {
if (id !== undefined) {
index = this.ids.indexOf(id);
if (index === -1)
throw new Error("ID not found");
}
if (index < 0 || index >= this.values.length) {
throw new Error("Index out of range");
}
this.values.splice(index, 1);
this.ids.splice(index, 1);
}
}
exports.ArrayWithIds = ArrayWithIds;
class RoundedArrayWithIds extends ArrayWithIds {
constructor(values = [], ids = [], options = ValueWithId_1.defaultRoundingOptions) {
super(values, ids);
this.roundingOptions = options;
}
toJSON() {
return this.values.map((value, index) => new ValueWithId_1.RoundedValueWithId(this.ids[index], value, this.roundingOptions).toJSON());
}
}
exports.RoundedArrayWithIds = RoundedArrayWithIds;
39 changes: 39 additions & 0 deletions dist/js/ValueWithId.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ObjectWithIdAndValueSchema } from "@mat3ra/esse/dist/js/types";
import { RoundingMethodEnum } from "./math";
interface ValueWithIdSchema<T> {
id: ObjectWithIdAndValueSchema["id"];
value: T | null;
}
export declare class ValueWithId<T> {
id: number;
value: T | null;
static defaultConfig: {
id: number;
value: null;
};
static fromValueAndId<U, C extends ValueWithId<U>>(this: new (args: {
id: number;
value: U;
}) => C, value: U, id?: number): C;
constructor({ id, value }?: ValueWithIdSchema<T>);
/**
* Converts the instance to a plain JavaScript object.
*/
toJSON(): object;
/**
* Checks if this instance is equal to another ValueWithId.
*/
equals<U>(other: ValueWithId<U>): boolean;
}
export interface RoundingOptions {
precision: number;
roundingMethod: RoundingMethodEnum;
}
export declare const defaultRoundingOptions: RoundingOptions;
export declare class RoundedValueWithId<T> extends ValueWithId<T> {
readonly precision: number;
readonly roundingMethod: RoundingMethodEnum;
constructor(id: number, value: T, options?: RoundingOptions);
toJSON(): object;
}
export {};
68 changes: 68 additions & 0 deletions dist/js/ValueWithId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoundedValueWithId = exports.defaultRoundingOptions = exports.ValueWithId = void 0;
const math_1 = require("./math");
class ValueWithId {
static fromValueAndId(value, id = 0) {
return new this({ id, value });
}
constructor({ id, value } = ValueWithId.defaultConfig) {
this.id = id;
this.value = value;
}
/**
* Converts the instance to a plain JavaScript object.
*/
toJSON() {
if (this.value !== null &&
typeof this.value === "object" &&
"toJSON" in this.value &&
typeof this.value.toJSON === "function") {
return { id: this.id, value: this.value.toJSON() };
}
return { id: this.id, value: this.value };
}
/**
* Checks if this instance is equal to another ValueWithId.
*/
equals(other) {
if (!(other instanceof ValueWithId))
return false;
// because U may differ from T, we cast to unknown when comparing
const v1 = this.value;
const v2 = other.value;
if (Array.isArray(v1) && Array.isArray(v2)) {
if (v1.length !== v2.length)
return false;
for (let i = 0; i < v1.length; i++) {
if (v1[i] !== v2[i])
return false;
}
return this.id === other.id;
}
return this.id === other.id && v1 === v2;
}
}
exports.ValueWithId = ValueWithId;
ValueWithId.defaultConfig = {
id: 0,
value: null,
};
exports.defaultRoundingOptions = {
precision: 9,
roundingMethod: math_1.RoundingMethodEnum.HalfAwayFromZero,
};
class RoundedValueWithId extends ValueWithId {
constructor(id, value, options = exports.defaultRoundingOptions) {
super({ id, value });
this.precision = options.precision;
this.roundingMethod = options.roundingMethod;
}
toJSON() {
return {
id: this.id,
value: math_1.math.roundArrayOrNumber(this.value, this.precision, this.roundingMethod),
};
}
}
exports.RoundedValueWithId = RoundedValueWithId;
79 changes: 50 additions & 29 deletions dist/js/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
export namespace coefficients {
let EV_TO_RY: number;
let BOHR_TO_ANGSTROM: number;
let ANGSTROM_TO_BOHR: number;
let EV_A_TO_RY_BOHR: number;
}
export namespace tolerance {
let length: number;
let lengthAngstrom: number;
let pointsDistance: number;
}
export namespace units {
let bohr: string;
let angstrom: string;
let degree: string;
let radian: string;
let alat: string;
}
export namespace ATOMIC_COORD_UNITS {
let crystal: string;
let cartesian: string;
}
export const HASH_TOLERANCE: 3;
declare namespace _default {
export { coefficients };
export { tolerance };
export { units };
export { ATOMIC_COORD_UNITS };
}
export declare const coefficients: {
EV_TO_RY: number;
BOHR_TO_ANGSTROM: number;
ANGSTROM_TO_BOHR: number;
EV_A_TO_RY_BOHR: number;
};
export declare const tolerance: {
length: number;
lengthAngstrom: number;
pointsDistance: number;
};
export declare const units: {
bohr: string;
angstrom: string;
degree: string;
radian: string;
alat: string;
};
/**
* @summary Coordinates units for a material's basis.
*/
export declare const ATOMIC_COORD_UNITS: {
crystal: string;
cartesian: string;
};
export declare const HASH_TOLERANCE = 3;
declare const _default: {
coefficients: {
EV_TO_RY: number;
BOHR_TO_ANGSTROM: number;
ANGSTROM_TO_BOHR: number;
EV_A_TO_RY_BOHR: number;
};
tolerance: {
length: number;
lengthAngstrom: number;
pointsDistance: number;
};
units: {
bohr: string;
angstrom: string;
degree: string;
radian: string;
alat: string;
};
ATOMIC_COORD_UNITS: {
crystal: string;
cartesian: string;
};
};
export default _default;
15 changes: 14 additions & 1 deletion dist/js/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { ArrayWithIds, RoundedArrayWithIds } from "./ArrayWithIds";
import * as context from "./context";
import * as entity from "./entity";
import * as utils from "./utils";
export declare const Code: {
import { RoundedValueWithId, ValueWithId } from "./ValueWithId";
import { RoundedVector3D, Vector3D } from "./vector";
export { ArrayWithIds, ValueWithId, RoundedValueWithId, RoundedArrayWithIds, RoundedVector3D, Vector3D, };
export { entity, context, utils };
declare const Code: {
ArrayWithIds: typeof ArrayWithIds;
ValueWithId: typeof ValueWithId;
RoundedArrayWithIds: typeof RoundedArrayWithIds;
RoundedValueWithId: typeof RoundedValueWithId;
RoundedVector3D: typeof RoundedVector3D;
Vector3D: typeof Vector3D;
entity: typeof entity;
context: typeof context;
utils: typeof utils;
};
export type CodeType = typeof Code;
export default Code;
23 changes: 21 additions & 2 deletions dist/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,31 @@ var __importStar = (this && this.__importStar) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Code = void 0;
exports.utils = exports.context = exports.entity = exports.Vector3D = exports.RoundedVector3D = exports.RoundedArrayWithIds = exports.RoundedValueWithId = exports.ValueWithId = exports.ArrayWithIds = void 0;
const ArrayWithIds_1 = require("./ArrayWithIds");
Object.defineProperty(exports, "ArrayWithIds", { enumerable: true, get: function () { return ArrayWithIds_1.ArrayWithIds; } });
Object.defineProperty(exports, "RoundedArrayWithIds", { enumerable: true, get: function () { return ArrayWithIds_1.RoundedArrayWithIds; } });
const context = __importStar(require("./context"));
exports.context = context;
const entity = __importStar(require("./entity"));
exports.entity = entity;
const utils = __importStar(require("./utils"));
exports.Code = {
exports.utils = utils;
const ValueWithId_1 = require("./ValueWithId");
Object.defineProperty(exports, "RoundedValueWithId", { enumerable: true, get: function () { return ValueWithId_1.RoundedValueWithId; } });
Object.defineProperty(exports, "ValueWithId", { enumerable: true, get: function () { return ValueWithId_1.ValueWithId; } });
const vector_1 = require("./vector");
Object.defineProperty(exports, "RoundedVector3D", { enumerable: true, get: function () { return vector_1.RoundedVector3D; } });
Object.defineProperty(exports, "Vector3D", { enumerable: true, get: function () { return vector_1.Vector3D; } });
const Code = {
ArrayWithIds: ArrayWithIds_1.ArrayWithIds,
ValueWithId: ValueWithId_1.ValueWithId,
RoundedArrayWithIds: ArrayWithIds_1.RoundedArrayWithIds,
RoundedValueWithId: ValueWithId_1.RoundedValueWithId,
RoundedVector3D: vector_1.RoundedVector3D,
Vector3D: vector_1.Vector3D,
entity,
context,
utils,
};
exports.default = Code;
Loading
Loading