From 30156288217326c13db675c31a91cc4cb439e819 Mon Sep 17 00:00:00 2001 From: Sofiya Tepikin Date: Sun, 31 Jul 2022 18:23:17 +0000 Subject: [PATCH] Chore/remove is object --- lib/import-export/import-from-csv.js | 13 +--------- lib/import-export/replace-with-null.js | 12 +++++++++ package.json | 1 - test/replace-with-null.spec.js | 36 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 lib/import-export/replace-with-null.js create mode 100644 test/replace-with-null.spec.js diff --git a/lib/import-export/import-from-csv.js b/lib/import-export/import-from-csv.js index 2a797a1..80e9bbc 100644 --- a/lib/import-export/import-from-csv.js +++ b/lib/import-export/import-from-csv.js @@ -1,5 +1,4 @@ import csvParser from 'csvtojson' -import isObject from 'isobject' import { getSchema, tryToImportWithDelete, @@ -7,6 +6,7 @@ import { updateCycleStartsForAllCycleDays, } from '../../db' import getColumnNamesForCsv from './get-csv-column-names' +import replaceWithNullIfAllPropertiesAreNull from './replace-with-null' import { LocalDate } from '@js-joda/core' import labels from '../../i18n/en/settings' @@ -74,17 +74,6 @@ function validateHeaders(headers) { function putNullForEmptySymptoms(data) { data.forEach(replaceWithNullIfAllPropertiesAreNull) - - function replaceWithNullIfAllPropertiesAreNull(obj) { - Object.keys(obj).forEach((key) => { - if (!isObject(obj[key])) return - if (Object.values(obj[key]).every((val) => val === null)) { - obj[key] = null - return - } - replaceWithNullIfAllPropertiesAreNull(obj[key]) - }) - } } function getDbType(modelProperties, path) { diff --git a/lib/import-export/replace-with-null.js b/lib/import-export/replace-with-null.js new file mode 100644 index 0000000..0772fdd --- /dev/null +++ b/lib/import-export/replace-with-null.js @@ -0,0 +1,12 @@ +const isObject = (obj) => obj === Object(obj) + +export default function replaceWithNullIfAllPropertiesAreNull(obj) { + Object.keys(obj).forEach((key) => { + if (!isObject(obj[key])) return + if (Object.values(obj[key]).every((val) => val === null)) { + obj[key] = null + return + } + replaceWithNullIfAllPropertiesAreNull(obj[key]) + }) +} diff --git a/package.json b/package.json index b47c676..5629a67 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "@react-native-community/push-notification-ios": "^1.8.0", "csvtojson": "^2.0.8", "i18next": "^21.8.14", - "isobject": "^4.0.0", "moment": "^2.29.4", "nodejs-mobile-react-native": "^0.6.2", "object-path": "^0.11.4", diff --git a/test/replace-with-null.spec.js b/test/replace-with-null.spec.js new file mode 100644 index 0000000..54ef4d6 --- /dev/null +++ b/test/replace-with-null.spec.js @@ -0,0 +1,36 @@ +import replaceWithNullIfAllPropertiesAreNull from '../lib/import-export/replace-with-null' +import chai from 'chai' + +const expect = chai.expect + +describe('replaceWithNullIfAllPropertiesAreNull', () => { + it('when data object has no null values, data object remains unchanged', () => { + const initialData = { + bleeding: { exclude: false, value: 2 }, + date: '2021-10-08', + } + const obj = { ...initialData } + replaceWithNullIfAllPropertiesAreNull(obj) + + expect(obj).to.deep.equal(initialData) + }) + + it('when data object has nested object with all values null, nested object is replaced with null', () => { + const initialData = { + bleeding: { exclude: null, value: null }, + date: '2021-10-08', + } + const obj = { ...initialData } + const expectedData = { bleeding: null, date: '2021-10-08' } + replaceWithNullIfAllPropertiesAreNull(obj) + + expect(obj).to.deep.equal(expectedData) + }) + + it('when data object is empty, data object remains unchanged', () => { + const obj = {} + replaceWithNullIfAllPropertiesAreNull(obj) + + expect(obj).to.deep.equal({}) + }) +})