Skip to content

Commit

Permalink
change quote
Browse files Browse the repository at this point in the history
  • Loading branch information
TA2k committed Sep 22, 2023
1 parent ddbcc54 commit 22b7d67
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 86 deletions.
142 changes: 71 additions & 71 deletions lib/json2iob.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ preferedArrayName //set key to use this as an array entry name
autoCast (true false) // make JSON.parse to parse numbers correctly
descriptions: Object of names for state keys
*/
const JSONbig = require("json-bigint")({ storeAsString: true });
const JSONbig = require('json-bigint')({ storeAsString: true });
module.exports = class Json2iob {
constructor(adapter) {
this.adapter = adapter;
Expand All @@ -18,7 +18,7 @@ module.exports = class Json2iob {
async parse(path, element, options) {
try {
if (element === null || element === undefined) {
this.adapter.log.debug("Cannot extract empty: " + path);
this.adapter.log.debug('Cannot extract empty: ' + path);
return;
}

Expand All @@ -28,21 +28,21 @@ module.exports = class Json2iob {
if (!options) {
options = { write: false };
} else {
options["write"] = false;
options['write'] = false;
}
}

if (typeof element === "string" || typeof element === "number") {
const lastPathElement = path.split(".").pop();
if (typeof element === 'string' || typeof element === 'number') {
const lastPathElement = path.split('.').pop();

if (!this.alreadyCreatedObjects[path]) {
await this.adapter
.setObjectNotExistsAsync(path, {
type: "state",
type: 'state',
common: {
name: lastPathElement,
role: this.getRole(element, options.write),
type: element !== null ? typeof element : "mixed",
type: element !== null ? typeof element : 'mixed',
write: options.write,
read: true,
},
Expand All @@ -62,9 +62,9 @@ module.exports = class Json2iob {
if (!this.alreadyCreatedObjects[path]) {
await this.adapter
.setObjectNotExistsAsync(path, {
type: "channel",
type: 'channel',
common: {
name: options.channelName || "",
name: options.channelName || '',
write: false,
read: true,
},
Expand All @@ -79,7 +79,7 @@ module.exports = class Json2iob {
});
}
if (Array.isArray(element)) {
await this.extractArray(element, "", path, options);
await this.extractArray(element, '', path, options);
return;
}

Expand All @@ -90,16 +90,16 @@ module.exports = class Json2iob {

if (Array.isArray(element[key])) {
await this.extractArray(element, key, path, options);
} else if (element[key] !== null && typeof element[key] === "object") {
await this.parse(path + "." + key, element[key], options);
} else if (element[key] !== null && typeof element[key] === 'object') {
await this.parse(path + '.' + key, element[key], options);
} else {
const pathKey = key.replace(/\./g, "_");
if (!this.alreadyCreatedObjects[path + "." + pathKey]) {
const pathKey = key.replace(/\./g, '_');
if (!this.alreadyCreatedObjects[path + '.' + pathKey]) {
let objectName = key;
if (options.descriptions && options.descriptions[key]) {
objectName = options.descriptions[key];
}
const type = element[key] !== null ? typeof element[key] : "mixed";
const type = element[key] !== null ? typeof element[key] : 'mixed';
const common = {
name: objectName,
role: this.getRole(element[key], options.write),
Expand All @@ -109,23 +109,23 @@ module.exports = class Json2iob {
};

await this.adapter
.setObjectNotExistsAsync(path + "." + pathKey, {
type: "state",
.setObjectNotExistsAsync(path + '.' + pathKey, {
type: 'state',
common: common,
native: {},
})
.then(() => {
this.alreadyCreatedObjects[path + "." + pathKey] = true;
this.alreadyCreatedObjects[path + '.' + pathKey] = true;
})
.catch((error) => {
this.adapter.log.error(error);
});
}
await this.adapter.setStateAsync(path + "." + pathKey, element[key], true);
await this.adapter.setStateAsync(path + '.' + pathKey, element[key], true);
}
}
} catch (error) {
this.adapter.log.error("Error extract keys: " + path + " " + JSON.stringify(element));
this.adapter.log.error('Error extract keys: ' + path + ' ' + JSON.stringify(element));
this.adapter.log.error(error);
}
}
Expand All @@ -138,29 +138,29 @@ module.exports = class Json2iob {
const arrayElement = element[index];
index = parseInt(index) + 1;
if (index < 10) {
index = "0" + index;
index = '0' + index;
}
let arrayPath = key + index;
if (typeof arrayElement === "string" && key !== "") {
await this.parse(path + "." + key + "." + arrayElement, arrayElement, options);
if (typeof arrayElement === 'string' && key !== '') {
await this.parse(path + '.' + key + '.' + arrayElement, arrayElement, options);
continue;
}
if (typeof arrayElement[Object.keys(arrayElement)[0]] === "string") {
if (typeof arrayElement[Object.keys(arrayElement)[0]] === 'string') {
arrayPath = arrayElement[Object.keys(arrayElement)[0]];
}
for (const keyName of Object.keys(arrayElement)) {
if (keyName.endsWith("Id") && arrayElement[keyName] !== null) {
if (keyName.endsWith('Id') && arrayElement[keyName] !== null) {
if (arrayElement[keyName] && arrayElement[keyName].replace) {
arrayPath = arrayElement[keyName].replace(/\./g, "");
arrayPath = arrayElement[keyName].replace(/\./g, '');
} else {
arrayPath = arrayElement[keyName];
}
}
}
for (const keyName in Object.keys(arrayElement)) {
if (keyName.endsWith("Name")) {
if (keyName.endsWith('Name')) {
if (arrayElement[keyName] && arrayElement[keyName].replace) {
arrayPath = arrayElement[keyName].replace(/\./g, "");
arrayPath = arrayElement[keyName].replace(/\./g, '');
} else {
arrayPath = arrayElement[keyName];
}
Expand All @@ -169,49 +169,49 @@ module.exports = class Json2iob {

if (arrayElement.id) {
if (arrayElement.id.replace) {
arrayPath = arrayElement.id.replace(/\./g, "");
arrayPath = arrayElement.id.replace(/\./g, '');
} else {
arrayPath = arrayElement.id;
}
}
if (arrayElement.name) {
arrayPath = arrayElement.name.replace(/\./g, "");
arrayPath = arrayElement.name.replace(/\./g, '');
}
if (arrayElement.label) {
arrayPath = arrayElement.label.replace(/\./g, "");
arrayPath = arrayElement.label.replace(/\./g, '');
}
if (arrayElement.labelText) {
arrayPath = arrayElement.labelText.replace(/\./g, "");
arrayPath = arrayElement.labelText.replace(/\./g, '');
}
if (arrayElement.start_date_time) {
arrayPath = arrayElement.start_date_time.replace(/\./g, "");
arrayPath = arrayElement.start_date_time.replace(/\./g, '');
}
if (options.preferedArrayName && options.preferedArrayName.indexOf("+") !== -1) {
const preferedArrayNameArray = options.preferedArrayName.split("+");
if (options.preferedArrayName && options.preferedArrayName.indexOf('+') !== -1) {
const preferedArrayNameArray = options.preferedArrayName.split('+');
if (arrayElement[preferedArrayNameArray[0]]) {
const element0 = arrayElement[preferedArrayNameArray[0]].replace(/\./g, "").replace(/ /g, "");
let element1 = "";
if (preferedArrayNameArray[1].indexOf("/") !== -1) {
const subArray = preferedArrayNameArray[1].split("/");
const element0 = arrayElement[preferedArrayNameArray[0]].replace(/\./g, '').replace(/ /g, '');
let element1 = '';
if (preferedArrayNameArray[1].indexOf('/') !== -1) {
const subArray = preferedArrayNameArray[1].split('/');
const subElement = arrayElement[subArray[0]];
if (subElement && subElement[subArray[1]] !== undefined) {
element1 = subElement[subArray[1]];
} else if (arrayElement[subArray[1]] !== undefined) {
element1 = arrayElement[subArray[1]];
}
} else {
element1 = arrayElement[preferedArrayNameArray[1]].replace(/\./g, "").replace(/ /g, "");
element1 = arrayElement[preferedArrayNameArray[1]].replace(/\./g, '').replace(/ /g, '');
}
arrayPath = element0 + "-" + element1;
arrayPath = element0 + '-' + element1;
}
} else if (options.preferedArrayName && options.preferedArrayName.indexOf("/") !== -1) {
const preferedArrayNameArray = options.preferedArrayName.split("/");
} else if (options.preferedArrayName && options.preferedArrayName.indexOf('/') !== -1) {
const preferedArrayNameArray = options.preferedArrayName.split('/');
const subElement = arrayElement[preferedArrayNameArray[0]];
if (subElement) {
arrayPath = subElement[preferedArrayNameArray[1]].replace(/\./g, "").replace(/ /g, "");
arrayPath = subElement[preferedArrayNameArray[1]].replace(/\./g, '').replace(/ /g, '');
}
} else if (options.preferedArrayName && arrayElement[options.preferedArrayName]) {
arrayPath = arrayElement[options.preferedArrayName].replace(/\./g, "");
arrayPath = arrayElement[options.preferedArrayName].replace(/\./g, '');
}

if (options.forceIndex) {
Expand All @@ -221,42 +221,42 @@ module.exports = class Json2iob {
if (
!options.forceIndex &&
Object.keys(arrayElement).length === 2 &&
typeof Object.keys(arrayElement)[0] === "string" &&
typeof Object.keys(arrayElement)[1] === "string" &&
typeof arrayElement[Object.keys(arrayElement)[0]] !== "object" &&
typeof arrayElement[Object.keys(arrayElement)[1]] !== "object" &&
arrayElement[Object.keys(arrayElement)[0]] !== "null"
typeof Object.keys(arrayElement)[0] === 'string' &&
typeof Object.keys(arrayElement)[1] === 'string' &&
typeof arrayElement[Object.keys(arrayElement)[0]] !== 'object' &&
typeof arrayElement[Object.keys(arrayElement)[1]] !== 'object' &&
arrayElement[Object.keys(arrayElement)[0]] !== 'null'
) {
let subKey = arrayElement[Object.keys(arrayElement)[0]];
const subValue = arrayElement[Object.keys(arrayElement)[1]];
const subName = Object.keys(arrayElement)[0] + " " + Object.keys(arrayElement)[1];
const subName = Object.keys(arrayElement)[0] + ' ' + Object.keys(arrayElement)[1];
if (key) {
subKey = key + "." + subKey;
subKey = key + '.' + subKey;
}
if (!this.alreadyCreatedObjects[path + "." + subKey]) {
if (!this.alreadyCreatedObjects[path + '.' + subKey]) {
await this.adapter
.setObjectNotExistsAsync(path + "." + subKey, {
type: "state",
.setObjectNotExistsAsync(path + '.' + subKey, {
type: 'state',
common: {
name: subName,
role: this.getRole(subValue, options.write),
type: subValue !== null ? typeof subValue : "mixed",
type: subValue !== null ? typeof subValue : 'mixed',
write: options.write,
read: true,
},
native: {},
})
.then(() => {
this.alreadyCreatedObjects[path + "." + subKey] = true;
this.alreadyCreatedObjects[path + '.' + subKey] = true;
});
}
await this.adapter.setStateAsync(path + "." + subKey, subValue, true);
await this.adapter.setStateAsync(path + '.' + subKey, subValue, true);
continue;
}
await this.parse(path + "." + arrayPath, arrayElement, options);
await this.parse(path + '.' + arrayPath, arrayElement, options);
}
} catch (error) {
this.adapter.log.error("Cannot extract array " + path);
this.adapter.log.error('Cannot extract array ' + path);
this.adapter.log.error(error);
}
}
Expand All @@ -269,21 +269,21 @@ module.exports = class Json2iob {
return true;
}
getRole(element, write) {
if (typeof element === "boolean" && !write) {
return "indicator";
if (typeof element === 'boolean' && !write) {
return 'indicator';
}
if (typeof element === "boolean" && write) {
return "switch";
if (typeof element === 'boolean' && write) {
return 'switch';
}
if (typeof element === "number" && !write) {
return "value";
if (typeof element === 'number' && !write) {
return 'value';
}
if (typeof element === "number" && write) {
return "level";
if (typeof element === 'number' && write) {
return 'level';
}
if (typeof element === "string") {
return "text";
if (typeof element === 'string') {
return 'text';
}
return "state";
return 'state';
}
};
6 changes: 3 additions & 3 deletions main.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";
'use strict';

/**
* This is a dummy TypeScript test file using chai and mocha
Expand All @@ -9,10 +9,10 @@

// tslint:disable:no-unused-expression

const { expect } = require("chai");
const { expect } = require('chai');
// import { functionToTest } from "./moduleToTest";

describe("module to test => function to test", () => {
describe('module to test => function to test', () => {
// initializing logic
const expected = 5;

Expand Down
6 changes: 3 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require("path");
const { tests } = require("@iobroker/testing");
const path = require('path');
const { tests } = require('@iobroker/testing');

// Run integration tests - See https://github.com/ioBroker/testing for a detailed explanation and further options
tests.integration(path.join(__dirname, ".."));
tests.integration(path.join(__dirname, '..'));
8 changes: 4 additions & 4 deletions test/mocha.setup.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Don't silently swallow unhandled rejections
process.on("unhandledRejection", (e) => {
process.on('unhandledRejection', (e) => {
throw e;
});

// enable the should interface with sinon
// and load chai-as-promised and sinon-chai by default
const sinonChai = require("sinon-chai");
const chaiAsPromised = require("chai-as-promised");
const { should, use } = require("chai");
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const { should, use } = require('chai');

should();
use(sinonChai);
Expand Down
4 changes: 2 additions & 2 deletions test/mocharc.custom.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"require": ["test/mocha.setup.js"],
"watch-files": ["!(node_modules|test)/**/*.test.js", "*.test.js", "test/**/test!(PackageFiles|Startup).js"]
"require": ["test/mocha.setup.js"],
"watch-files": ["!(node_modules|test)/**/*.test.js", "*.test.js", "test/**/test!(PackageFiles|Startup).js"]
}
6 changes: 3 additions & 3 deletions test/package.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require("path");
const { tests } = require("@iobroker/testing");
const path = require('path');
const { tests } = require('@iobroker/testing');

// Validate the package files
tests.packageFiles(path.join(__dirname, ".."));
tests.packageFiles(path.join(__dirname, '..'));

0 comments on commit 22b7d67

Please sign in to comment.