Skip to content

Commit

Permalink
fix: update reset
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Nov 17, 2024
1 parent 28cc7e9 commit f94e709
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 277 deletions.
50 changes: 49 additions & 1 deletion projects/aas-core/src/lib/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export function parseDate(s: string, localeId?: string): Date | undefined {
getSeconds(timeTuple?.items),
);
} else {
date = new Date(NaN);
date = new Date(0);
}
} else {
date = new Date(s);
Expand Down Expand Up @@ -496,6 +496,54 @@ export function parseDate(s: string, localeId?: string): Date | undefined {
}
}

/**
* Indicates whether the specified date is valid.
* @param value The date value.
* @returns `true` if the date value is valid; otherwise, `false`.
*/
export function isValidDate(value: Date | undefined): boolean {
if (value === undefined) {
return false;
}

const year = value.getFullYear();
if (year < 1970 || year > 3000) {
return false;
}

const month = value.getMonth();
if (month < 0 || month > 11) {
return false;
}

const day = value.getDay();
if (day < 1 || day > 31) {
return false;
}

const hours = value.getHours();
if (hours < 0 || hours > 23) {
return false;
}

const minutes = value.getMinutes();
if (minutes < 0 || minutes > 59) {
return false;
}

const seconds = value.getSeconds();
if (seconds < 0 || seconds > 59) {
return false;
}

const ms = value.getMilliseconds();
if (ms < 0 || ms > 999) {
return false;
}

return true;
}

/**
* Determines the data type from the specified string expression.
* @param value The value or a string expression.
Expand Down
15 changes: 15 additions & 0 deletions projects/aas-core/src/test/convert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
toBoolean,
mimeTypeToExtension,
extensionToMimeType,
isValidDate,
} from '../lib/convert.js';

describe('Convert', () => {
Expand Down Expand Up @@ -334,6 +335,20 @@ describe('Convert', () => {
const date = new Date(2023, 1, 27, 13, 14, 15).toString();
expect(parseDate(date)).toEqual(new Date(2023, 1, 27, 13, 14, 15));
});

it('converts "2023-03-22"', () => {
expect(parseDate('2023-03-22')).toEqual(new Date('2023-03-22'));
});
});

describe('isValidDate', () => {
it('indicates that IEC 61984 is invalid', () => {
expect(isValidDate(new Date('IEC 61984'))).toEqual(false);
});

it('indicates that 2023-03-22 is valid', () => {
expect(isValidDate(new Date('2023-03-22'))).toEqual(true);
});
});

describe('toLocale', () => {
Expand Down
1 change: 1 addition & 0 deletions projects/aas-lib/src/lib/index-change.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class IndexChangeService {
break;
case 'Reset':
this.reset.emit();
this.state.set({ changedDocuments: 0, documentCount: 0, endpointCount: 0 });
break;
}
}
Expand Down
6 changes: 4 additions & 2 deletions projects/aas-server/src/app/aas-index/aas-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
baseType,
getAbbreviation,
isProperty,
isValidDate,
parseDate,
parseNumber,
toBoolean,
Expand Down Expand Up @@ -76,7 +77,7 @@ export abstract class AASIndex {

public abstract clear(): Promise<void>;

public abstract reset(): Promise<void>;
public abstract destroy(): Promise<void>;

protected toAbbreviation(referable: aas.Referable): string {
return getAbbreviation(referable.modelType)!.toLowerCase();
Expand Down Expand Up @@ -123,7 +124,8 @@ export abstract class AASIndex {
return undefined;
}

return parseDate(referable.value);
const value = parseDate(referable.value);
return isValidDate(value) ? value : undefined;
}

protected toBooleanValue(referable: aas.Referable): boolean | undefined {
Expand Down
Loading

0 comments on commit f94e709

Please sign in to comment.