Skip to content

Support non ascii #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions string-inflection.el
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ the beginning."
:group 'string-inflection
:type 'boolean)

(defconst string-inflection-word-chars "a-zA-Z0-9_-")
(defconst string-inflection-word-chars "[:lower:][:upper:][:digit:]_-")

(defcustom string-inflection-erase-chars-when-region "./"
"When selected in the region, this character is included in the transformation
Expand Down Expand Up @@ -281,8 +281,8 @@ selected. If include `:', select `FOO::VERSION' to run
(defun string-inflection-underscore-function (str)
"FooBar => foo_bar"
(let ((case-fold-search nil))
(setq str (replace-regexp-in-string "\\([a-z0-9]\\)\\([A-Z]\\)" "\\1_\\2" str))
(setq str (replace-regexp-in-string "\\([A-Z]+\\)\\([A-Z][a-z]\\)" "\\1_\\2" str))
(setq str (replace-regexp-in-string "\\([[:lower:][:digit:]]\\)\\([[:upper:]]\\)" "\\1_\\2" str))
(setq str (replace-regexp-in-string "\\([[:upper:]]+\\)\\([[:upper:]][[:lower:]]\\)" "\\1_\\2" str))
(setq str (replace-regexp-in-string "-" "_" str)) ; FOO-BAR => FOO_BAR
(setq str (replace-regexp-in-string "_+" "_" str))
(downcase str)))
Expand Down Expand Up @@ -374,33 +374,33 @@ selected. If include `:', select `FOO::VERSION' to run
(defun string-inflection-word-p (str)
"if foo => t"
(let ((case-fold-search nil))
(string-match "\\`[a-z0-9]+\\'" str)))
(string-match "\\`[[:lower:][:digit:]]+\\'" str)))

(defun string-inflection-underscore-p (str)
"if foo_bar => t"
(let ((case-fold-search nil))
(string-match "\\`[a-z0-9_]+\\'" str)))
(string-match "\\`[[:lower:][:digit:]_]+\\'" str)))

(defun string-inflection-upcase-p (str)
"if FOO_BAR => t"
(let ((case-fold-search nil))
(string-match "\\`[A-Z0-9_]+\\'" str)))
(string-match "\\`[[:upper:][:digit:]_]+\\'" str)))

(defun string-inflection-pascal-case-p (str)
"if FooBar => t"
(let ((case-fold-search nil))
(and
(string-match "[a-z]" str)
(string-match "\\`[A-Z][a-zA-Z0-9]+\\'" str))))
(string-match "[[:lower:]]" str)
(string-match "\\`[[:upper:]][[:lower:][:upper:][:digit:]]+\\'" str))))

(fset 'string-inflection-upper-camelcase-p 'string-inflection-pascal-case-p)

(defun string-inflection-camelcase-p (str)
"if fooBar => t"
(let ((case-fold-search nil))
(and
(string-match "[A-Z]" str)
(string-match "\\`[a-z][a-zA-Z0-9]+\\'" str))))
(string-match "[[:upper:]]" str)
(string-match "\\`[[:lower:]][[:lower:][:upper:][:digit:]]+\\'" str))))

(fset 'string-inflection-lower-camelcase-p 'string-inflection-camelcase-p)

Expand All @@ -412,9 +412,8 @@ selected. If include `:', select `FOO::VERSION' to run
"if Foo_Bar => t"
(let ((case-fold-search nil))
(and
(string-match "[A-Z]" str)
(string-match "_" str)
(string-match "\\`[A-Z][a-zA-Z0-9_]+\\'" str))))
(string-match "\\`[[:upper:]][[:lower:][:upper:][:digit:]_]+\\'" str))))

(provide 'string-inflection)
;;; string-inflection.el ends here
109 changes: 82 additions & 27 deletions test/string-inflection-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -51,67 +51,103 @@
(should (equal "ends_with_php" (string-inflection-underscore-function "EndsWithPHP")))
(should (equal "php_and_xml_too" (string-inflection-underscore-function "PHPAndXMLToo")))
(should (equal "php_and_xml_too" (string-inflection-underscore-function "phpAndXmlToo")))
(should (equal "ph_pand_xm_ltoo" (string-inflection-underscore-function "PHPandXMLtoo"))))
(should (equal "ph_pand_xm_ltoo" (string-inflection-underscore-function "PHPandXMLtoo")))
(should (equal "eĥo_ŝanĝo_ĉiu_ĵaŭde" (string-inflection-underscore-function "EĤOŜanĝoĈIUĴaŭde"))))

(ert-deftest test-pascal-case ()
(should (equal "Foo1Bar" (string-inflection-pascal-case-function "Foo1Bar")))
(should (equal "Eĥo1Ŝanĝo" (string-inflection-pascal-case-function "Eĥo1Ŝanĝo")))
(should (equal "Foo1Bar" (string-inflection-pascal-case-function "FOO1_BAR")))
(should (equal "Eĥo1Ŝanĝo" (string-inflection-pascal-case-function "EĤO1_ŜANĜO")))
(should (equal "Foo1Bar" (string-inflection-pascal-case-function "FOO1__BAR")))
(should (equal "Foo" (string-inflection-pascal-case-function "foo"))))
(should (equal "Eĥo1Ŝanĝo" (string-inflection-pascal-case-function "EĤO1__ŜANĜO")))
(should (equal "Foo" (string-inflection-pascal-case-function "foo")))
(should (equal "Eĥo" (string-inflection-pascal-case-function "eĥo"))))

(ert-deftest test-lower-camelcase ()
(should (equal "fooBar" (string-inflection-camelcase-function "FooBar")))
(should (equal "foo1Bar" (string-inflection-camelcase-function "FOO1BAR"))))
(should (equal "eĥoŜanĝo" (string-inflection-camelcase-function "EĥoŜanĝo")))
(should (equal "foo1Bar" (string-inflection-camelcase-function "FOO1BAR")))
(should (equal "eĥo1Ŝanĝo" (string-inflection-camelcase-function "EĤO1ŜANĜO"))))

(ert-deftest test-kebab-case ()
(should (equal "foo-bar" (string-inflection-kebab-case-function "FooBar"))))
(should (equal "foo-bar" (string-inflection-kebab-case-function "FooBar")))
(should (equal "eĥo-ŝanĝo" (string-inflection-kebab-case-function "EĥoŜanĝo"))))

(ert-deftest test-toggle ()
(should (equal "FooBar" (string-inflection-toggle-function "foo_bar")))
(should (equal "EĥoŜanĝo" (string-inflection-toggle-function "eĥo_ŝanĝo")))
(should (equal "fooBar" (string-inflection-toggle-function "FooBar")))
(should (equal "foo_bar" (string-inflection-toggle-function "FOO_BAR"))))
(should (equal "eĥoŜanĝo" (string-inflection-toggle-function "EĥoŜanĝo")))
(should (equal "ĉirkaŭIro" (string-inflection-toggle-function "ĈirkaŭIro")))
(should (equal "foo_bar" (string-inflection-toggle-function "FOO_BAR")))
(should (equal "eĥo_ŝanĝo" (string-inflection-toggle-function "EĤO_ŜANĜO"))))

(ert-deftest test-upcase ()
(should (equal "FOO1_BAR" (string-inflection-upcase-function "foo1_bar")))
(should (equal "EĤO1_ŜANĜO" (string-inflection-upcase-function "eĥo1_ŝanĝo")))
(should (equal "FOO1_BAR" (string-inflection-upcase-function "Foo1Bar")))
(should (equal "FOO_BAR" (string-inflection-upcase-function "Foo_bar"))))
(should (equal "EĤO1_ŜANĜO" (string-inflection-upcase-function "Eĥo1Ŝanĝo")))
(should (equal "FOO_BAR" (string-inflection-upcase-function "Foo_bar")))
(should (equal "EĤO_ŜANĜO" (string-inflection-upcase-function "Eĥo_ŝanĝo"))))

(ert-deftest test-capital-underscore ()
(should (equal "Foo1_Bar" (string-inflection-capital-underscore-function "foo1_bar")))
(should (equal "Eĥo1_Ŝanĝo" (string-inflection-capital-underscore-function "eĥo1_ŝanĝo")))
(should (equal "Foo1_Bar" (string-inflection-capital-underscore-function "FOO1_BAR")))
(should (equal "Eĥo1_Ŝanĝo" (string-inflection-capital-underscore-function "EĤO1_ŜANĜO")))
(should (equal "Foo1bar" (string-inflection-capital-underscore-function "foo1bar")))
(should (equal "Eĥo1ŝanĝo" (string-inflection-capital-underscore-function "eĥo1ŝanĝo")))
(should (equal "Foo1_Bar" (string-inflection-capital-underscore-function "foo1__bar")))
(should (equal "Eĥo1_Ŝanĝo" (string-inflection-capital-underscore-function "eĥo1__ŝanĝo")))
(should (equal "Foo1_Bar" (string-inflection-capital-underscore-function "Foo1Bar")))
(should (equal "Eĥo1_Ŝanĝo" (string-inflection-capital-underscore-function "Eĥo1Ŝanĝo")))
(should (equal "Foo1_Bar" (string-inflection-capital-underscore-function "FOO1Bar")))
(should (equal "Foo_Bar" (string-inflection-capital-underscore-function "FOO_BAR"))))
(should (equal "Eĥo1_Ŝanĝo" (string-inflection-capital-underscore-function "EĤO1Ŝanĝo")))
(should (equal "Foo_Bar" (string-inflection-capital-underscore-function "FOO_BAR")))
(should (equal "Eĥo_Ŝanĝo" (string-inflection-capital-underscore-function "EĤO_ŜANĜO"))))

;; --------------------------------------------------------------------------------

(ert-deftest test-word-p ()
(should-not (equal nil (string-inflection-word-p "foo")))
(should (equal nil (string-inflection-word-p "foo_bar"))))
(should (string-inflection-word-p "foo"))
(should (string-inflection-word-p "eĥo"))
(should-not (string-inflection-word-p "foo_bar"))
(should-not (string-inflection-word-p "eĥo_ŝanĝo")))

(ert-deftest test-underscore-p ()
(should-not (equal nil (string-inflection-underscore-p "foo")))
(should-not (equal nil (string-inflection-underscore-p "foo_bar"))))
(should (string-inflection-underscore-p "foo"))
(should (string-inflection-underscore-p "eĥo"))
(should (string-inflection-underscore-p "foo_bar"))
(should (string-inflection-underscore-p "eĥo_ŝanĝo")))

(ert-deftest test-pascal-case-p ()
(should-not (equal nil (string-inflection-pascal-case-p "Foo")))
(should-not (equal nil (string-inflection-pascal-case-p "FooBar"))))
(should (string-inflection-pascal-case-p "Foo"))
(should (string-inflection-pascal-case-p "Eĥo"))
(should (string-inflection-pascal-case-p "FooBar"))
(should (string-inflection-pascal-case-p "EĥoŜanĝo"))
(should-not (string-inflection-pascal-case-p "FOO"))
(should (string-inflection-pascal-case-p "Eĥĥ")))

(ert-deftest test-camelcase-p ()
(should (equal nil (string-inflection-camelcase-p "foo")))
(should-not (equal nil (string-inflection-camelcase-p "fooBar"))))
(should-not (string-inflection-camelcase-p "foo"))
(should-not (string-inflection-camelcase-p "eĥo"))
(should (string-inflection-camelcase-p "fooBar"))
(should (string-inflection-camelcase-p "eĥoŜanĝo")))

(ert-deftest test-lower-upcase-p ()
(should-not (equal nil (string-inflection-upcase-p "FOO")))
(should-not (equal nil (string-inflection-upcase-p "FOO_BAR"))))
(should (string-inflection-upcase-p "FOO"))
(should (string-inflection-upcase-p "EĤO"))
(should (string-inflection-upcase-p "FOO_BAR"))
(should (string-inflection-upcase-p "EĤO_ŜANĜO")))

(ert-deftest test-kebab-case-p ()
(should-not (equal nil (string-inflection-kebab-case-p "foo-bar"))))
(should (string-inflection-kebab-case-p "foo-bar"))
(should (string-inflection-kebab-case-p "eĥo-ŝanĝo")))

(ert-deftest test-capital-underscore-p ()
(should-not (equal nil (string-inflection-capital-underscore-p "Foo_Bar"))))
(should (string-inflection-capital-underscore-p "Foo_Bar"))
(should (string-inflection-capital-underscore-p "Eĥo_Ŝanĝo")))

;; -------------------------------------------------------------------------------- Target word of cursor position

Expand All @@ -125,10 +161,14 @@

(ert-deftest test-get-current-word-on-cursor ()
(should (equal "foo" (buffer-try "foo" '(point-max))))
(should (equal "eĥo" (buffer-try "eĥo" '(point-max))))
(should (equal "foo" (buffer-try "foo" '(point-min))))
(should (equal "eĥo" (buffer-try "eĥo" '(point-min))))
(should (equal "" (buffer-try "" '(point-max))))
(should (equal "foo" (buffer-try "foo->bar" '(point-min))))
(should (equal "eĥo" (buffer-try "eĥo->ŝanĝo" '(point-min))))
(should (equal "foo-" (buffer-try "foo-" '(point-min))))
(should (equal "eĥo-" (buffer-try "eĥo-" '(point-min))))
)

;; -------------------------------------------------------------------------------- Target all of region
Expand Down Expand Up @@ -161,17 +201,32 @@
(should (equal " a " (region-try " a ")))
(should (equal "a\n" (region-try "a\n")))
(should (equal "a\nb\n" (region-try "a\nb\n")))

(should (equal "eĥo_ŝanĝo" (region-try "eĥo_ŝanĝo")))
)

(defun buffer-try-inflect (str inflect)
(with-temp-buffer
(insert str)
(goto-char (point-min))
(funcall inflect)
(buffer-string)))

;; https://github.com/akicho8/string-inflection/issues/30
(ert-deftest test-buffer-underscore ()
(should (equal "object_name->method"
(with-current-buffer (get-buffer-create "*test*")
(insert "objectName->method")
(goto-char (point-min))
(string-inflection-underscore)
(prog1
(buffer-string)
(kill-this-buffer))))))
(should (equal "object_name->method" (buffer-try-inflect "objectName->method" 'string-inflection-underscore)))
(should (equal "object1_name->method" (buffer-try-inflect "object1Name->method" 'string-inflection-underscore)))
(should (equal "eĥo_ŝanĝo->ĉiuĴaŭde" (buffer-try-inflect "eĥoŜanĝo->ĉiuĴaŭde" 'string-inflection-underscore))))

(ert-deftest test-buffer-camelcase ()
(should (equal "ObjectName->method" (buffer-try-inflect "object_name->method" 'string-inflection-camelcase)))
(should (equal "Object1Name->method" (buffer-try-inflect "object1_name->method" 'string-inflection-camelcase)))
(should (equal "EĥoŜanĝo->ĉiuĴaŭde" (buffer-try-inflect "eĥo_ŝanĝo->ĉiuĴaŭde" 'string-inflection-camelcase))))

(ert-deftest test-buffer-lower-camelcase ()
(should (equal "objectName->method" (buffer-try-inflect "object_name->method" 'string-inflection-lower-camelcase)))
(should (equal "object1Name->method" (buffer-try-inflect "object1_name->method" 'string-inflection-lower-camelcase)))
(should (equal "eĥoŜanĝo->ĉiuĴaŭde" (buffer-try-inflect "eĥo_ŝanĝo->ĉiuĴaŭde" 'string-inflection-lower-camelcase))))


(ert-run-tests-batch t)