Skip to content

Commit

Permalink
Rename SimObject_Metadata_set to ConvertYamlStringToSimObject_Metadat…
Browse files Browse the repository at this point in the history
…a for clarity and update references accordingly
  • Loading branch information
77it committed Jan 21, 2025
1 parent fc41ce7 commit e969ebe
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/modules/_utils/metadata_utils.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export { SimObject_Metadata_set };
export { ConvertYamlStringToSimObject_Metadata };

import { SimObject_Metadata } from '../../engine/simobject/parts/simobject_metadata.js';
import { schema, sanitize, parseYAML, isNullOrWhiteSpace } from '../../deps.js';

/** Get a string, parse it as YAML, return an instance of SimObject_Metadata.
* Init set the class from a string.
* The string will be probably read from settings, and must be in YAML format (object or array of objects):
* [{ type: c/c, value: bank, weight: 0.2}, {type: line of business, value: canteen, weight: 0.3 }]
* { type: c/c, value: bank, weight: 0.2}
* If the creation of `SimObject_Metadata` class fails, is set to empty arrays.
* @param {string} source
* @returns {SimObject_Metadata}
*/
function SimObject_Metadata_set (source) {
function ConvertYamlStringToSimObject_Metadata (source) {
// parse source as YAML
const _parsed = parseYAML(source);

Expand Down
1 change: 1 addition & 0 deletions src/modules/_utils/search_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ModuleData, isNullOrWhiteSpace, sanitize, eq2, get2, parseJsonToLocalDa
* Optionally sanitize the result; if no match is found, return undefined, optionally sanitized.
* If `string_insensitive_match` is true: `tableName` and `lookup_value` (if string) are matched in a case insensitive & trim way,
* `lookup_column` and `return_column` are get directly and if not found they are matched with all keys in a case insensitive & trim way.
* `return_first_match` if true, return the first match, otherwise the last match.
* @param {ModuleData} moduleData
* @param {{tableName: string, lookup_value: *, lookup_column: string, return_column: string, return_first_match?: boolean, string_insensitive_match?: boolean, sanitization?: string, sanitizationOptions?: Object }} opt
* return_first_match default = true;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/genericmovements.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Agenda } from './_utils/Agenda.js';
import { sanitizeModuleData } from './_utils/sanitization_utils.js';
import { moduleDataLookup, searchDateKeys } from './_utils/search_utils.js';
import { SimObject_Metadata } from '../engine/simobject/parts/simobject_metadata.js';
import { SimObject_Metadata_set } from './_utils/metadata_utils.js';
import { ConvertYamlStringToSimObject_Metadata } from './_utils/metadata_utils.js';
import { ModuleData, SimulationContext, schema, sanitize, eq2, get2, isNullOrWhiteSpace, mergeNewKeys } from '../deps.js';

export class Module {
Expand Down Expand Up @@ -101,7 +101,7 @@ export class Module {

// read from Settings ACTIVE_UNIT & ACTIVE_METADATA and save the values
this.#ACTIVE_UNIT = this.#simulationContext.getSetting({ name: SETTINGS_NAMES.Simulation.ACTIVE_UNIT });
this.#activeMetadata = SimObject_Metadata_set(this.#simulationContext.getSetting({ unit: this.#ACTIVE_UNIT, name: SETTINGS_NAMES.Simulation.ACTIVE_METADATA }));
this.#activeMetadata = ConvertYamlStringToSimObject_Metadata(this.#simulationContext.getSetting({ unit: this.#ACTIVE_UNIT, name: SETTINGS_NAMES.Simulation.ACTIVE_METADATA }));

// init Agenda with #ACTIVE_UNIT & reading from settings $$SIMULATION_START_DATE__LAST_HISTORICAL_DAY_IS_THE_DAY_BEFORE
this.#agenda = new Agenda({ simulationStartDate: this.#simulationContext.getSetting({ unit: this.#ACTIVE_UNIT, name: SETTINGS_NAMES.Unit.$$SIMULATION_START_DATE__LAST_HISTORICAL_DAY_IS_THE_DAY_BEFORE }) });
Expand Down
10 changes: 5 additions & 5 deletions test/modules_test/utils/metadata_utils__test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SimObject_Metadata_set } from '../../../src/modules/_utils/metadata_utils.js';
import { ConvertYamlStringToSimObject_Metadata } from '../../../src/modules/_utils/metadata_utils.js';
import { SimObject_Metadata } from '../../../src/engine/simobject/parts/simobject_metadata.js';

import { test } from 'node:test';
Expand All @@ -8,21 +8,21 @@ import { eqObj } from '../../deps.js';

t('ActiveMetadata tests - invalid input', async () => {
//@ts-ignore
const metadata = SimObject_Metadata_set(null);
const metadata = ConvertYamlStringToSimObject_Metadata(null);
assert(eqObj(metadata, { metadata__Name: [], metadata__Value: [], metadata__PercentageWeight: [] }));
});

t('ActiveMetadata tests - setup with object', async () => {
const metadata = SimObject_Metadata_set('{ type: c/c, value: bank, weight: 0.2}');
const metadata = ConvertYamlStringToSimObject_Metadata('{ type: c/c, value: bank, weight: 0.2}');
assert(eqObj(metadata, { metadata__Name: ['c/c'], metadata__Value: ['bank'], metadata__PercentageWeight: [0.2] }));
});

t('ActiveMetadata tests - setup with array', async () => {
const metadata = SimObject_Metadata_set('[{ type: c/c, value: bank, weight: 0.2}, {type: line of business, value: canteen, weight: 0.3 }]');
const metadata = ConvertYamlStringToSimObject_Metadata('[{ type: c/c, value: bank, weight: 0.2}, {type: line of business, value: canteen, weight: 0.3 }]');
assert(eqObj(metadata, { metadata__Name: ['c/c', 'line of business'], metadata__Value: ['bank', 'canteen'], metadata__PercentageWeight: [0.2, 0.3] }));
});

t('ActiveMetadata tests - setup with array, with sanitization of values', async () => {
const metadata = SimObject_Metadata_set('[{ type: c/c, value: 0, weight: 0.2}, {type: line of business, value: canteen, weight: "0.3" }]');
const metadata = ConvertYamlStringToSimObject_Metadata('[{ type: c/c, value: 0, weight: 0.2}, {type: line of business, value: canteen, weight: "0.3" }]');
assert(eqObj(metadata, { metadata__Name: ['c/c', 'line of business'], metadata__Value: ['0', 'canteen'], metadata__PercentageWeight: [0.2, 0.3] }));
});
5 changes: 2 additions & 3 deletions test/modules_test/utils/search_utils__test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { xlookup, moduleDataLookup, searchDateKeys } from '../../../src/modules/_utils/search_utils.js';
import { eq2 } from '../../../src/lib/obj_utils.js';

import { schema } from '../../deps.js';
import { ModuleData } from '../../../src/engine/modules/module_data.js';
Expand Down Expand Up @@ -234,7 +233,7 @@ t('searchDateKeys test', async () => {
assert.deepStrictEqual(searchDateKeys({ obj, prefix: '' }), exp);
}

{ // test $$HISTORICAL_COLUMN_PREFIX (also case insensitive)
{ // test $$HISTORICAL_COLUMN_PREFIX (also case insensitive detection of the prefix)
const obj = {
'h#2023-12-25': 1,
b: 2, // ignored, not starting with prefix
Expand All @@ -245,7 +244,7 @@ t('searchDateKeys test', async () => {

const exp = [
{ key: 'H#2023/01/29', date: new Date(2023, 0, 29) },
{ key: 'h#2023-12-25', date: new Date(2023, 11, 25) }, // case insensitive
{ key: 'h#2023-12-25', date: new Date(2023, 11, 25) }, // case insensitive detection of the prefix
];

assert.deepStrictEqual(searchDateKeys({ obj: obj, prefix: SETTINGS_DEFAULT_VALUES[SETTINGS_NAMES.$$HISTORICAL_COLUMN_PREFIX] }), exp);
Expand Down

0 comments on commit e969ebe

Please sign in to comment.