Repository: platers/obsidian-linter (ts)

Issue:
Bug: New "Format yaml array sections" toggle puts single Date value into array
## Describe the Bug

With the new "Format YAML Array" and "Format yaml array sections" rules, a single non-array date value in the YAML is getting converted into an array. I do not understand why!

## How to Reproduce

In "Settings" -> "Linter" turn on the first toggle under "Format YAML Array" and the "Format yaml array sections" toggle. Run the linter on the following: 

``` markdown
---
date: 2022-08-14
tags: []
---

```

Result is:
``` markdown
---
date: [2022-08-14]
tags: []
---

```

This breaks all my dataview queries that use the `date` field, since they expect it to be a Date and not an array.

## Expected Behavior

No changes made by linter. Expected output:

``` markdown
---
date: 2022-08-14
tags: []
---

```

## Device

- [x] Desktop


Pull request:
Fix Issue with Some Date Formats Being Treated as Arrays
Fixes #352 

Changes Made:
- Added a UT to account for the scenario in question
- Added a check to make sure that the object in question for the yaml was not a date before going ahead and converting the value to the specified array format

Patch:
diff --git a/src/rules/format-yaml-arrays.ts b/src/rules/format-yaml-arrays.ts
index 2ed5b4e..e621e5c 100644
--- a/src/rules/format-yaml-arrays.ts
+++ b/src/rules/format-yaml-arrays.ts
@@ -48,6 +48,11 @@ export default class RuleTemplate extends RuleBuilder<FormatYamlArrayOptions> {
         return text;
       }
 
+      // https://stackoverflow.com/a/44198641
+      const isValidDate = function(date: any) {
+        return date && Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date);
+      };
+
       if (options.formatAliasKey && Object.keys(yaml).includes(obsidianAliasKey)) {
         text = setYamlSection(text, obsidianAliasKey, formatYamlArrayValue(convertAliasValueToStringOrStringArray(splitValueIfSingleOrMultilineArray(getYamlSectionValue(text, obsidianAliasKey))), options.aliasArrayStyle));
       }
@@ -61,7 +66,7 @@ export default class RuleTemplate extends RuleBuilder<FormatYamlArrayOptions> {
 
         for (const key of Object.keys(yaml)) {
           // skip non-arrays and already accounted for keys
-          if (keysToIgnore.includes(key) || !(typeof yaml[key] === 'object' && yaml[key] as string[])) {
+          if (keysToIgnore.includes(key) || !(typeof yaml[key] === 'object' && !isValidDate(yaml[key]) && yaml[key] as string[])) {
             continue;
           }
 


Test patch:
diff --git a/__tests__/format-yaml-arrays.test.ts b/__tests__/format-yaml-arrays.test.ts
index 0ee68e2..2e8f7fc 100644
--- a/__tests__/format-yaml-arrays.test.ts
+++ b/__tests__/format-yaml-arrays.test.ts
@@ -768,5 +768,21 @@ ruleTest({
         ---
       `,
     },
+    {
+      // accounts for https://github.com/platers/obsidian-linter/issues/352
+      testName: 'Date stays the same',
+      before: dedent`
+        ---
+        date: 2022-08-14
+        tags: []
+        ---
+      `,
+      after: dedent`
+        ---
+        date: 2022-08-14
+        tags: []
+        ---
+      `,
+    },
   ],
 });