From 7f8c83b3aa5e8547bfd92980e73c103b2e9080b7 Mon Sep 17 00:00:00 2001 From: Willi Eggeling Date: Fri, 22 Feb 2019 21:53:45 +0100 Subject: [PATCH] added configuration option for Item name casing, refactoring (#133) - the casing of generated Item names from Thing labels can now be configured using the extension's configuration. - the ItemsProvider.ts file has been refactored to not depend on the 'underscore.strings' library any longer. fixes #132 Signed-off-by: Willi Eggeling --- CHANGELOG.md | 2 ++ client/package.json | 1 - client/src/ThingsExplorer/ItemsProvider.ts | 37 ++++++++++++++-------- client/src/Utils.ts | 20 ++++++++++++ package.json | 9 ++++++ 5 files changed, 55 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fbdd22..06f59e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - removed settings param 'restCompletions' - renamed settings param 'lspEnabled' to 'remoteLspEnabled' - renamed settings param 'lspPort' to 'remoteLspPort' +- added settings param 'itemCasing' to allow for Item format configuration (#133) +- removed reference to library 'underscore.string' (#133) ## 0.4.1 - 2018-12-09 - Fixed Basic UI Preview (#117) diff --git a/client/package.json b/client/package.json index 4a1e385..bed9f66 100644 --- a/client/package.json +++ b/client/package.json @@ -23,7 +23,6 @@ "lodash": "^4.17.4", "request": "^2.83.0", "request-promise-native": "^1.0.5", - "underscore.string": "^3.3.5", "vscode": "^1.1.26", "vscode-languageclient": "^5.2.1" } diff --git a/client/src/ThingsExplorer/ItemsProvider.ts b/client/src/ThingsExplorer/ItemsProvider.ts index f5b0b9b..1166c61 100644 --- a/client/src/ThingsExplorer/ItemsProvider.ts +++ b/client/src/ThingsExplorer/ItemsProvider.ts @@ -1,23 +1,35 @@ import { SnippetString, - window + window, + workspace } from 'vscode' import * as _ from 'lodash' -import * as s from 'underscore.string' import * as AsciiTable from 'ascii-table' import { Thing } from './Thing' import { Channel } from './Channel' +import { humanize } from '../Utils' + +/** generate an Item name from a Thing label by using the configured casing */ +function generateItemName(label: string) : string { + const config = workspace.getConfiguration('openhab'); + switch(config.get('itemCasing')) { + case 'snake': + // upper snake case, 'Guest Room Light' -> 'Guest_Room_Light' + return _.startCase(label).replace(/ /g, "_"); + default: + // camel case, 'Guest Room Light' -> 'GuestRoomLight' + return _.startCase(label).replace(/ /g, ""); + } +} const CHANNEL_TEMPLATE = (channel: Channel): SnippetString => { if (channel.kind === 'STATE') { - let name = s.classify(channel.id); - let label = channel.label || s(channel.id) - .replaceAll('#', ' ') - .humanize() - .value() - + const name = generateItemName(channel.id); + const label = humanize( + (channel.label || channel.id).replace(/#/g, ' ') + ); return new SnippetString( `${channel.itemType} ${name} "${label}" {channel="${channel.uid}"}\n` ) @@ -32,11 +44,10 @@ const THING_TEMPLATE = (thing: Thing): SnippetString => { let channels = _(thing.channels) .filter(channel => channel.kind === 'STATE') .map((channel: Channel) => { - let name = s.classify(thing.label) + '_' + s.classify(channel.id); - let label = channel.label || s(channel.id) - .replaceAll('#', ' ') - .humanize() - .value() + const name = generateItemName(`${thing.label} ${channel.id}`); + const label = humanize( + (channel.label || channel.id).replace(/#/g, ' ') + ); return [channel.itemType, name, `"${label}"`, `{channel="${channel.uid}"}`] }) .value() diff --git a/client/src/Utils.ts b/client/src/Utils.ts index 5ff54e0..229f435 100644 --- a/client/src/Utils.ts +++ b/client/src/Utils.ts @@ -10,6 +10,26 @@ import {PreviewPanel} from './WebView/PreviewPanel' import * as _ from 'lodash' import * as request from 'request-promise-native' +/** + * humanize function adapter from the previously included underscore.string library + */ +export function humanize(str: string) : string { + return _.upperFirst( + // original 'underscored' of underscore.string + str.trim() + .replace(/([a-z\d])([A-Z]+)/g, '$1_$2') + .replace(/[-\s]+/g, '_') + .toLowerCase() + .replace(/([a-z\d])([A-Z]+)/g, '$1_$2') + .replace(/_id$/, '') + .replace(/_/g, ' ') + // original 'humanize' of underscore.string + .replace(/_id$/, '') + .replace(/_/g, ' ') + .trim() + ); +} + export function getHost() { let config = workspace.getConfiguration('openhab') let host = config.host diff --git a/package.json b/package.json index 1dfd92f..7c77387 100644 --- a/package.json +++ b/package.json @@ -237,6 +237,15 @@ ], "default": "basicui", "description": "(optional) Choose between `basicui` and `classicui` for the sitemap preview panel" + }, + "openhab.itemCasing": { + "type": "string", + "default": "camel", + "enum": [ + "camel", + "snake" + ], + "markdownDescription": "Choose how the `Create Items from Channels` command generates Item names. Use `camel` for `CamelCase` or `snake` for `Upper_Snake_Case`." } } },