-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rkessler/semantic units #259
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*********************************************************** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License | ||
**********************************************************/ | ||
import * as fs from 'fs'; | ||
|
||
export interface StringMap<T> { | ||
[key: string]: T; | ||
} | ||
|
||
export interface SemanticUnit { | ||
displayName: string | StringMap<string>; | ||
abbreviation: string; | ||
} | ||
|
||
export const generateSemanticUnitDigest = () => { | ||
const rawSemanticUnitsFileLocation = './src/app/shared/units/semanticUnitsListRaw.json'; | ||
const rawSemanticUnitsFileContents = fs.readFileSync(rawSemanticUnitsFileLocation, 'utf-8'); | ||
const rawSemanticUnitsFileObject = JSON.parse(rawSemanticUnitsFileContents); | ||
|
||
const semanticUnits: StringMap<SemanticUnit> = {}; | ||
|
||
const minDtmiLength = 4; | ||
const extensionTypeIndex = 2; | ||
const extensionType = 'unit'; | ||
const unitNameIndex = 3; | ||
|
||
// tslint:disable-next-line: no-any | ||
rawSemanticUnitsFileObject['@graph'].forEach((entry: any) => { | ||
const dtmi = entry['@id'].split(':'); | ||
if (dtmi.length >= minDtmiLength && dtmi[extensionTypeIndex].toLowerCase() === extensionType) { | ||
const unitName = dtmi[unitNameIndex].split(';')[0]; | ||
const displayName = entry.displayName; | ||
const abbreviation = entry.abbreviation || entry.symbol; | ||
|
||
semanticUnits[unitName] = { | ||
abbreviation, | ||
displayName | ||
}; | ||
} | ||
}); | ||
|
||
const semanticUnitsFileLocation = './src/app/shared/units/semanticUnitsList.json'; | ||
const semanticUnitsFileContents = JSON.stringify(semanticUnits); | ||
fs.writeFileSync(semanticUnitsFileLocation, semanticUnitsFileContents); | ||
}; | ||
|
||
generateSemanticUnitDigest(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/*********************************************************** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License | ||
**********************************************************/ | ||
export interface StringMap<T> { | ||
[key: string]: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/app/shared/units/components/__snapshots__/semanticUnits.spec.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SemanticUnit matches snapshot when unit host is undefined 1`] = ` | ||
<span> | ||
-- | ||
</span> | ||
`; | ||
|
||
exports[`SemanticUnit matches snapshot when unit is listed 1`] = ` | ||
<span | ||
title="displayName" | ||
> | ||
displayName | ||
( | ||
abbr | ||
) | ||
</span> | ||
`; | ||
|
||
exports[`SemanticUnit matches snapshot when unit is undefined 1`] = ` | ||
<span> | ||
-- | ||
</span> | ||
`; | ||
|
||
exports[`SemanticUnit matches snapshot when unit is unlisted 1`] = ` | ||
<span> | ||
fizzbin | ||
</span> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/*********************************************************** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License | ||
**********************************************************/ | ||
import * as React from 'react'; | ||
import { getSemanticUnit } from '../semanticUnits'; | ||
import { getLocalizedData } from '../../../api/dataTransforms/modelDefinitionTransform'; | ||
|
||
export interface SemanticUnitProps { | ||
unitHost?: {unit?: string}; | ||
} | ||
|
||
export const SemanticUnit: React.FC<SemanticUnitProps> = props => { | ||
const { unitHost } = props; | ||
const emptyUnit = '--'; | ||
if (!unitHost || !unitHost.unit) { | ||
return <span>{emptyUnit}</span>; | ||
} | ||
|
||
const semanticUnit = getSemanticUnit(unitHost.unit); | ||
if (!semanticUnit) { | ||
return <span>{unitHost.unit}</span>; | ||
} else { | ||
const title = getLocalizedData(semanticUnit.displayName); | ||
return <span title={title}>{title} ({semanticUnit.abbreviation})</span>; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/*********************************************************** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License | ||
**********************************************************/ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { SemanticUnit } from './semanticUnit'; | ||
import * as SemanticUnits from '../semanticUnits'; | ||
|
||
describe('SemanticUnit', () => { | ||
it('matches snapshot when unit host is undefined', () => { | ||
expect(shallow(<SemanticUnit unitHost={undefined}/>)).toMatchSnapshot(); | ||
|
||
}); | ||
|
||
it('matches snapshot when unit is undefined', () => { | ||
expect(shallow(<SemanticUnit unitHost={{}}/>)).toMatchSnapshot(); | ||
|
||
}); | ||
|
||
it('matches snapshot when unit is unlisted', () => { | ||
expect(shallow(<SemanticUnit unitHost={{unit: 'fizzbin'}}/>)).toMatchSnapshot(); | ||
|
||
}); | ||
|
||
it('matches snapshot when unit is listed', () => { | ||
const spy = jest.spyOn(SemanticUnits, 'getSemanticUnit'); | ||
spy.mockReturnValue({ | ||
abbreviation: 'abbr', | ||
displayName: 'displayName' | ||
}); | ||
|
||
expect(shallow(<SemanticUnit unitHost={{unit: 'fizzbin'}}/>)).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/*********************************************************** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License | ||
**********************************************************/ | ||
import { getSemanticUnit, semanticUnits } from './semanticUnits'; | ||
|
||
describe('getSemanticUnit', () => { | ||
it('returns undefined when unit name is undefined', () => { | ||
expect(getSemanticUnit(undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('returns undefined when unit name is unlisted', () => { | ||
expect(getSemanticUnit('fizzbin')).toEqual(undefined); | ||
}); | ||
|
||
it('returns listed semantic unit with expected value', () => { | ||
semanticUnits.fizzbin = { | ||
abbreviation: 'abbr', | ||
displayName: 'displayName' | ||
}; | ||
|
||
expect(getSemanticUnit('fizzbin')).toEqual({ | ||
abbreviation: 'abbr', | ||
displayName: 'displayName' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/*********************************************************** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License | ||
**********************************************************/ | ||
import { StringMap } from '../../api/models/stringMap'; | ||
|
||
export interface SemanticUnit { | ||
displayName: string | StringMap<string>; | ||
abbreviation: string; | ||
} | ||
|
||
export const semanticUnits = require('./semanticUnitsList.json'); // tslint:disable-line: no-var-requires | ||
export const getSemanticUnit = (unitName: string | undefined): SemanticUnit | undefined => { | ||
return unitName && semanticUnits[unitName]; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me confirm if I've understand it correctly. This 'import-semantic-units' is not included in any npm build script, because the output json is already included in the source code.
This script is for the future in case we need re-generate the json.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly. I suspect the they'll do updates over time -- like adding localization.