Skip to content

Commit

Permalink
Improved MorningstarSearchParams.
Browse files Browse the repository at this point in the history
  • Loading branch information
bre1470 committed Aug 1, 2024
1 parent 72434bf commit 942fc61
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
71 changes: 49 additions & 22 deletions src/Shared/MorningstarSearchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,33 @@ export class MorningstarSearchParams extends URLSearchParams {


/**
* Sets `id` and `idType` based on given security options.
* Sets the parameter with the given date.
*
* @param options
* Security options to set.
* @param name
* Parameter name to set.
*
* @param date
* JavaScript timestamp or date string.
*
* @return
* The modified search parameters as reference.
*/
public setSecurityOptions(
options: Array<MorningstarSecurityOptions>
public setDate(
name: ('endDate'|'startDate'),
date: (number|string)
): MorningstarSearchParams {
let id: string = this.get('id') || '';
let idType: string = this.get('idType') || '';

options = (options instanceof Array ? options : [options]);

for (const security of options) {
id = (id ? `${id}|${security.id}` : id);
idType = (idType ? `${idType}|${security.idType}` : idType);
}
const dateTime = (
typeof date === 'number' ?
new Date(date) :
new Date(Date.parse(date))
);

this.set('id', id);
this.set('idType', idType);
this.set(name, dateTime.toISOString().substring(0, 10));

return this;
}


/**
* Sets `languageId` based on given localization options.
*
Expand All @@ -84,13 +84,40 @@ export class MorningstarSearchParams extends URLSearchParams {
options: LocalizationOptions
): MorningstarSearchParams {

const {
country,
language
} = options;
this.set(
'languageId', (
options.language.toLowerCase() +
'-' +
options.country.toUpperCase()
)
);

const languageCultureCode = `${language.toLowerCase()}-${country.toUpperCase()}`;
this.set('languageId', languageCultureCode);
return this;
}


/**
* Sets `id` and `idType` based on given security options.
*
* @param options
* Security options to set.
*
* @return
* The modified search parameters as reference.
*/
public setSecurityOptions(
options: Array<MorningstarSecurityOptions>
): MorningstarSearchParams {
let id: string = this.get('id') || '';
let idType: string = this.get('idType') || '';

for (const security of options) {
id = (id ? `${id}|${security.id}` : security.id);
idType = (idType ? `${idType}|${security.idType}` : security.idType);
}

this.set('id', id);
this.set('idType', idType);

return this;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Shared/MorningstarURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class MorningstarURL extends URL {

super(superURL);

this.searchParams = new MorningstarSearchParams(superURL.searchParams);
this._searchParams = new MorningstarSearchParams(superURL.searchParams);
}


Expand All @@ -64,7 +64,12 @@ export class MorningstarURL extends URL {
* */


public override searchParams: MorningstarSearchParams;
public _searchParams: MorningstarSearchParams;


public override get searchParams(): MorningstarSearchParams {
return this._searchParams;
}


}
Expand Down
8 changes: 6 additions & 2 deletions src/Shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@


import MorningstarAPI from './MorningstarAPI';
import MorningstarOAuth2 from './MorningstarAccess';
import MorningstarError from './MorningstarError';
import MorningstarAccess from './MorningstarAccess';
import MorningstarRegion from './MorningstarRegion';


/* *
Expand All @@ -48,5 +50,7 @@ export * from './MorningstarOptions';

export default {
MorningstarAPI,
MorningstarOAuth2
MorningstarError,
MorningstarAccess,
MorningstarRegion
};

0 comments on commit 942fc61

Please sign in to comment.