Repository: yamatoiizuka/palt-typesetting (ts)

Issue:
Prevent thin space insertion around whitespace characters
スペースやタブ、その他制御文字（改行を行う `\n` など）の前後に四分アキを入れない

Pull request:
Prevent thin space insertion around whitespace characters
Fix #117 

Patch:
diff --git a/src/insert-separators.ts b/src/insert-separators.ts
index 6024a74..e124d90 100644
--- a/src/insert-separators.ts
+++ b/src/insert-separators.ts
@@ -2,6 +2,7 @@ import LineBreaker from 'linebreak'
 import { CharClass, LanguageClass } from './util-text-classes'
 import { createWbr, createThinSpace } from './util-tags'
 import { TypesettingOptions } from '../types'
+import { whitespaceRegex } from './util-regex'
 
 /**
  * HTMLテキストノードにセパレーター（四分アキ、<wbr>）を挿入します。
@@ -62,6 +63,8 @@ const addSeparatorsToSegment = (current: string, next = '', options: Typesetting
  * @return 四分アキを追加すべきかどうか
  */
 const shouldAddThinSpace = (current: string, next: string): boolean => {
+  if (whitespaceRegex.test(current) || whitespaceRegex.test(next)) return false
+
   return LanguageClass.shouldAddThinSpace(current, next) || CharClass.shouldAddThinSpace(current, next)
 }
 
diff --git a/src/util-regex.ts b/src/util-regex.ts
index d51e812..84760b1 100644
--- a/src/util-regex.ts
+++ b/src/util-regex.ts
@@ -40,3 +40,8 @@ export const periodsRegex = /。|．/
 export const noBreakRulesRegex = new RegExp(
   `[${'—‥…＿'}${String.fromCharCode(0x2500)}-${String.fromCharCode(0x257f)}]+`
 )
+
+/**
+ * 空白文字、制御文字
+ */
+export const whitespaceRegex = /[\s\u00A0]/


Test patch:
diff --git a/tests/insert-separators.test.ts b/tests/insert-separators.test.ts
index 5de9f86..de1dc9d 100644
--- a/tests/insert-separators.test.ts
+++ b/tests/insert-separators.test.ts
@@ -21,6 +21,27 @@ describe('insertSeparators', () => {
     expect(insertSeparatorsToText(currentText, nextText, options)).toEqual(expected)
   })
 
+  it('does not insert a thin space before a space', () => {
+    const currentText = 'です。'
+    const nextText = ' '
+    const expected = 'です。'
+    expect(insertSeparatorsToText(currentText, nextText, options)).toEqual(expected)
+  })
+
+  it('does not insert a thin space after a space', () => {
+    const currentText = ' '
+    const nextText = '「こんにちは」'
+    const expected = ' <wbr>'
+    expect(insertSeparatorsToText(currentText, nextText, options)).toEqual(expected)
+  })
+
+  it('does not insert a thin space before a line fead', () => {
+    const currentText = 'です。'
+    const nextText = '\n'
+    const expected = 'です。'
+    expect(insertSeparatorsToText(currentText, nextText, options)).toEqual(expected)
+  })
+
   it('inserts a thin space between Japanese–Cyrillic', () => {
     const currentText = 'こんにちはДобрий день'
     const nextText = ''