Repository: maizzle/framework
Pull request #1123 (2023-12-29 13:41:01)

revert: object merging
Undo #1058 as it caused data duplication, explained in https://github.com/maizzle/framework/issues/1057#issuecomment-1872076756



Patch:
diff --git a/src/commands/serve.js b/src/commands/serve.js
index 8161905..b8d0dc4 100644
--- a/src/commands/serve.js
+++ b/src/commands/serve.js
@@ -6,8 +6,8 @@ const Config = require('../generators/config')
 const buildToFile = require('../commands/build')
 const renderToString = require('../functions/render')
 
-const {get, isObject} = require('lodash')
-const {clearConsole, merge} = require('../utils/helpers')
+const {get, merge, isObject} = require('lodash')
+const {clearConsole} = require('../utils/helpers')
 
 /**
  * Initialize Browsersync on-demand
diff --git a/src/generators/config.js b/src/generators/config.js
index 5020c87..7253c79 100644
--- a/src/generators/config.js
+++ b/src/generators/config.js
@@ -1,5 +1,6 @@
 const path = require('path')
-const {merge, requireUncached} = require('../utils/helpers')
+const {merge} = require('lodash')
+const {requireUncached} = require('../utils/helpers')
 
 const baseConfigFileNames = [
   './maizzle.config.js',
diff --git a/src/generators/output/to-disk.js b/src/generators/output/to-disk.js
index 5856f7b..6bd115f 100644
--- a/src/generators/output/to-disk.js
+++ b/src/generators/output/to-disk.js
@@ -1,12 +1,11 @@
 const path = require('path')
 const fs = require('fs-extra')
 const glob = require('glob-promise')
-const {get, isEmpty} = require('lodash')
-const {merge} = require('../../utils/helpers')
+const {get, merge, isEmpty} = require('lodash')
 
 const Config = require('../config')
-const Tailwind = require('../tailwindcss')
 const Plaintext = require('../plaintext')
+const Tailwind = require('../tailwindcss')
 
 const render = require('./to-string')
 
diff --git a/src/generators/output/to-string.js b/src/generators/output/to-string.js
index 2c4bcd0..c5ae868 100644
--- a/src/generators/output/to-string.js
+++ b/src/generators/output/to-string.js
@@ -1,10 +1,9 @@
 const fm = require('front-matter')
-const {get} = require('lodash')
+const Config = require('../config')
+const {get, merge} = require('lodash')
 const posthtml = require('../posthtml')
-const {merge} = require('../../utils/helpers')
-const Transformers = require('../../transformers')
 const Tailwind = require('../tailwindcss')
-const Config = require('../config')
+const Transformers = require('../../transformers')
 
 module.exports = async (html, options) => {
   process.env.NODE_ENV = get(options, 'maizzle.env', 'local')
diff --git a/src/generators/plaintext.js b/src/generators/plaintext.js
index 4890b56..0461688 100644
--- a/src/generators/plaintext.js
+++ b/src/generators/plaintext.js
@@ -1,7 +1,6 @@
 const path = require('path')
-const {get} = require('lodash')
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
+const {get, merge} = require('lodash')
 const {stripHtml} = require('string-strip-html')
 const defaultConfig = require('./posthtml/defaultConfig')
 
diff --git a/src/generators/posthtml/index.js b/src/generators/posthtml/index.js
index e38b468..1932656 100644
--- a/src/generators/posthtml/index.js
+++ b/src/generators/posthtml/index.js
@@ -1,9 +1,8 @@
 const fm = require('front-matter')
 const posthtml = require('posthtml')
-const {get, omit} = require('lodash')
 const fetch = require('posthtml-fetch')
 const layouts = require('posthtml-extend')
-const {merge} = require('../../utils/helpers')
+const {get, merge, omit} = require('lodash')
 const components = require('posthtml-component')
 const defaultPosthtmlConfig = require('./defaultConfig')
 const defaultComponentsConfig = require('./defaultComponentsConfig')
diff --git a/src/generators/tailwindcss.js b/src/generators/tailwindcss.js
index 2473549..e992c80 100644
--- a/src/generators/tailwindcss.js
+++ b/src/generators/tailwindcss.js
@@ -3,9 +3,9 @@ const fs = require('fs-extra')
 const postcss = require('postcss')
 const tailwindcss = require('tailwindcss')
 const postcssImport = require('postcss-import')
-const {get, isObject, isEmpty} = require('lodash')
+const {get, merge, isObject, isEmpty} = require('lodash')
 const postcssNested = require('tailwindcss/nesting')
-const {merge, requireUncached} = require('../utils/helpers')
+const {requireUncached} = require('../utils/helpers')
 const mergeLonghand = require('postcss-merge-longhand')
 const defaultComponentsConfig = require('./posthtml/defaultComponentsConfig')
 
diff --git a/src/transformers/attributeToStyle.js b/src/transformers/attributeToStyle.js
index 274d264..58039bc 100644
--- a/src/transformers/attributeToStyle.js
+++ b/src/transformers/attributeToStyle.js
@@ -1,8 +1,14 @@
+const {
+  get,
+  merge,
+  forEach,
+  intersection,
+  keys,
+  isEmpty
+} = require('lodash')
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
 const parseAttrs = require('posthtml-attrs-parser')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
-const {get, forEach, intersection, keys, isEmpty} = require('lodash')
 
 module.exports = async (html, config = {}, direct = false) => {
   const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
diff --git a/src/transformers/baseUrl.js b/src/transformers/baseUrl.js
index 269d422..8eccd7e 100644
--- a/src/transformers/baseUrl.js
+++ b/src/transformers/baseUrl.js
@@ -1,8 +1,7 @@
 const posthtml = require('posthtml')
 const isUrl = require('is-url-superb')
-const {merge} = require('../utils/helpers')
 const baseUrl = require('posthtml-base-url')
-const {get, isObject, isEmpty} = require('lodash')
+const {get, merge, isObject, isEmpty} = require('lodash')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
 module.exports = async (html, config = {}, direct = false) => {
diff --git a/src/transformers/extraAttributes.js b/src/transformers/extraAttributes.js
index 3b33be8..2fc8f2a 100644
--- a/src/transformers/extraAttributes.js
+++ b/src/transformers/extraAttributes.js
@@ -1,6 +1,5 @@
 const posthtml = require('posthtml')
-const {get, isObject} = require('lodash')
-const {merge} = require('../utils/helpers')
+const {get, merge, isObject} = require('lodash')
 const addAttributes = require('posthtml-extra-attributes')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/transformers/filters/index.js b/src/transformers/filters/index.js
index 959ba0e..5305f09 100644
--- a/src/transformers/filters/index.js
+++ b/src/transformers/filters/index.js
@@ -1,6 +1,5 @@
 const posthtml = require('posthtml')
-const {get, omit, has} = require('lodash')
-const {merge} = require('../../utils/helpers')
+const {get, merge, omit, has} = require('lodash')
 const defaultFilters = require('./defaultFilters')
 const PostCSS = require('../../generators/postcss')
 const posthtmlContent = require('posthtml-content')
diff --git a/src/transformers/markdown.js b/src/transformers/markdown.js
index ff44668..43397e0 100644
--- a/src/transformers/markdown.js
+++ b/src/transformers/markdown.js
@@ -1,6 +1,5 @@
-const {get} = require('lodash')
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
+const {get, merge} = require('lodash')
 const markdown = require('posthtml-markdownit')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/transformers/posthtmlMso.js b/src/transformers/posthtmlMso.js
index 7b1c84c..f7b589f 100644
--- a/src/transformers/posthtmlMso.js
+++ b/src/transformers/posthtmlMso.js
@@ -1,7 +1,6 @@
-const {get} = require('lodash')
 const posthtml = require('posthtml')
+const {get, merge} = require('lodash')
 const outlook = require('posthtml-mso')
-const {merge} = require('../utils/helpers')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
 module.exports = async (html, config) => {
diff --git a/src/transformers/prettify.js b/src/transformers/prettify.js
index f761823..7106179 100644
--- a/src/transformers/prettify.js
+++ b/src/transformers/prettify.js
@@ -1,7 +1,6 @@
 /* eslint-disable camelcase */
 const pretty = require('pretty')
-const {merge} = require('../utils/helpers')
-const {get, isEmpty, isObject} = require('lodash')
+const {get, merge, isEmpty, isObject} = require('lodash')
 
 module.exports = async (html, config = {}, direct = false) => {
   const defaultConfig = {
diff --git a/src/transformers/preventWidows.js b/src/transformers/preventWidows.js
index 5a88fac..5a0e3d9 100644
--- a/src/transformers/preventWidows.js
+++ b/src/transformers/preventWidows.js
@@ -1,6 +1,5 @@
 const posthtml = require('posthtml')
-const {get, isEmpty} = require('lodash')
-const {merge} = require('../utils/helpers')
+const {get, merge, isEmpty} = require('lodash')
 const {removeWidows} = require('string-remove-widows')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/transformers/removeAttributes.js b/src/transformers/removeAttributes.js
index 3654d29..b55122b 100644
--- a/src/transformers/removeAttributes.js
+++ b/src/transformers/removeAttributes.js
@@ -1,6 +1,5 @@
-const {get} = require('lodash')
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
+const {get, merge} = require('lodash')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
 module.exports = async (html, config = {}, direct = false) => {
diff --git a/src/transformers/removeInlineBackgroundColor.js b/src/transformers/removeInlineBackgroundColor.js
index c865337..777f99c 100644
--- a/src/transformers/removeInlineBackgroundColor.js
+++ b/src/transformers/removeInlineBackgroundColor.js
@@ -1,7 +1,7 @@
 const posthtml = require('posthtml')
-const {get, isEmpty} = require('lodash')
+const {get, merge, isEmpty} = require('lodash')
 const parseAttrs = require('posthtml-attrs-parser')
-const {merge, toStyleString} = require('../utils/helpers')
+const {toStyleString} = require('../utils/helpers')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
 module.exports = async (html, config = {}, direct = false) => {
diff --git a/src/transformers/removeInlineSizes.js b/src/transformers/removeInlineSizes.js
index b02b86c..5f497f5 100644
--- a/src/transformers/removeInlineSizes.js
+++ b/src/transformers/removeInlineSizes.js
@@ -1,7 +1,7 @@
 const posthtml = require('posthtml')
-const {get, isEmpty} = require('lodash')
+const {get, merge, isEmpty} = require('lodash')
 const parseAttrs = require('posthtml-attrs-parser')
-const {merge, toStyleString} = require('../utils/helpers')
+const {toStyleString} = require('../utils/helpers')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
 module.exports = async (html, config = {}, direct = false) => {
diff --git a/src/transformers/removeInlinedSelectors.js b/src/transformers/removeInlinedSelectors.js
index 534615d..0623d56 100644
--- a/src/transformers/removeInlinedSelectors.js
+++ b/src/transformers/removeInlinedSelectors.js
@@ -1,7 +1,6 @@
 const postcss = require('postcss')
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
-const {get, has, remove} = require('lodash')
+const {get, merge, has, remove} = require('lodash')
 const parseAttrs = require('posthtml-attrs-parser')
 const matchHelper = require('posthtml-match-helper')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
diff --git a/src/transformers/removeUnusedCss.js b/src/transformers/removeUnusedCss.js
index 81576a2..9de85ce 100644
--- a/src/transformers/removeUnusedCss.js
+++ b/src/transformers/removeUnusedCss.js
@@ -1,6 +1,5 @@
 const {comb} = require('email-comb')
-const {merge} = require('../utils/helpers')
-const {get, isEmpty, isObject} = require('lodash')
+const {get, merge, isEmpty, isObject} = require('lodash')
 const removeInlinedClasses = require('./removeInlinedSelectors')
 
 module.exports = async (html, config = {}, direct = false) => {
diff --git a/src/transformers/safeClassNames.js b/src/transformers/safeClassNames.js
index ad51cda..3019316 100644
--- a/src/transformers/safeClassNames.js
+++ b/src/transformers/safeClassNames.js
@@ -1,6 +1,5 @@
 const posthtml = require('posthtml')
-const {get, isEmpty} = require('lodash')
-const {merge} = require('../utils/helpers')
+const {get, merge, isEmpty} = require('lodash')
 const safeClassNames = require('posthtml-safe-class-names')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/transformers/shorthandInlineCSS.js b/src/transformers/shorthandInlineCSS.js
index 4f355c0..45fbb25 100644
--- a/src/transformers/shorthandInlineCSS.js
+++ b/src/transformers/shorthandInlineCSS.js
@@ -1,6 +1,5 @@
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
-const {get, isObject, isEmpty} = require('lodash')
+const {get, merge, isObject, isEmpty} = require('lodash')
 const mergeInlineLonghand = require('posthtml-postcss-merge-longhand')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/transformers/sixHex.js b/src/transformers/sixHex.js
index 5ea361c..145e0c0 100644
--- a/src/transformers/sixHex.js
+++ b/src/transformers/sixHex.js
@@ -1,6 +1,5 @@
-const {get} = require('lodash')
 const posthtml = require('posthtml')
-const {merge} = require('../utils/helpers')
+const {get, merge} = require('lodash')
 const {conv} = require('color-shorthand-hex-to-six-digit')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/transformers/urlParameters.js b/src/transformers/urlParameters.js
index ef4916a..56e1629 100644
--- a/src/transformers/urlParameters.js
+++ b/src/transformers/urlParameters.js
@@ -1,6 +1,5 @@
 const posthtml = require('posthtml')
-const {get, isEmpty} = require('lodash')
-const {merge} = require('../utils/helpers')
+const {get, merge, isEmpty} = require('lodash')
 const urlParams = require('posthtml-url-parameters')
 const defaultConfig = require('../generators/posthtml/defaultConfig')
 
diff --git a/src/utils/helpers.js b/src/utils/helpers.js
index cb58e3f..85684da 100644
--- a/src/utils/helpers.js
+++ b/src/utils/helpers.js
@@ -1,5 +1,3 @@
-const {mergeWith} = require('lodash')
-
 module.exports = {
   requireUncached: module => {
     try {
@@ -11,24 +9,5 @@ module.exports = {
   },
   // https://github.com/lukeed/console-clear
   clearConsole: () => process.stdout.write('\x1B[H\x1B[2J'),
-  toStyleString: (object = {}) => Object.entries(object).map(([k, v]) => `${k}: ${v}`).join('; '),
-  merge: (...objects) => {
-    if (objects.length < 2) {
-      return objects[0]
-    }
-
-    let merged = {}
-
-    for (const object of objects) {
-      merged = mergeWith(merged, object, (existingValue, newValue) => {
-        if (Array.isArray(existingValue) && Array.isArray(newValue)) {
-          return [...existingValue, ...newValue]
-        }
-
-        return undefined
-      })
-    }
-
-    return merged
-  }
+  toStyleString: (object = {}) => Object.entries(object).map(([k, v]) => `${k}: ${v}`).join('; ')
 }


Test patch:
diff --git a/test/test-misc.js b/test/test-misc.js
index bb31afd..84c35a5 100644
--- a/test/test-misc.js
+++ b/test/test-misc.js
@@ -6,24 +6,3 @@ test('requires an uncached module', t => {
   const helpers = h.requireUncached(path.resolve(process.cwd(), 'src/utils/helpers'))
   t.is(typeof helpers.requireUncached, 'function')
 })
-
-test('merges multiple objects containing arrays', t => {
-  const object1 = {foo: 'bar', arr: [1, 2]}
-  const object2 = {arr: [3, 4], baz: 'qux'}
-  const object3 = {what: 'ever', arr: [5, 6]}
-  const merged = h.merge(object1, object2, object3)
-
-  t.deepEqual(merged, {
-    arr: [1, 2, 3, 4, 5, 6],
-    baz: 'qux',
-    foo: 'bar',
-    what: 'ever'
-  })
-})
-
-test('returns object when less than two objects are provided', t => {
-  const object = {arr: [1, 2]}
-  const merged = h.merge(object)
-
-  t.deepEqual(merged, object)
-})