-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recase.art
107 lines (91 loc) · 2.89 KB
/
recase.art
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;===============================================
; Recase.art
;
; Unicode-friendly string case converter
; for Arturo
;
; MIT License
; (c) 2024 Yanis Zafirópulos
;===============================================
export module [
;---------------------------
; Helpers
;---------------------------
allCaps?: function [s][
and? 1 < size s
every? s 'ch -> or? [upper? ch]
[numeric? ch]
]
;---------------------------
; Private methods
;---------------------------
separated: method [s][
; crazy regex credit:
; https://stackoverflow.com/a/62527131/1270812
prepared: s | replace {/(*UTF8)([^[\p{L}\d]+|(?<=[\p{Ll}\d])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{Ll}\d])|(?<=[\p{L}\d])(?=\p{Lu}[\p{Ll}\d]))/} " "
| split.words
return map prepared 'word [
(\allCaps? word)? -> word
-> lower word
]
]
titleCase: method [s][
join map \separated s => capitalize
]
camelCase: method [s][
join map.with:'i \separated s 'x [
(i=0)? -> x
-> capitalize x
]
]
snakeCase: method [s][
join.with:"_" \separated s
]
kebabCase: method [s][
join.with:"-" \separated s
]
plainCase: method [s][
join.with:" " \separated s
]
;---------------------------
; Public methods
;---------------------------
recase: method.public [str :string][
;; description: « convert case of given string
;; options: [
;; title: « convert to TitleCase
;; camel: « convert to camelCase
;; snake: « convert to snake_case
;; kebab: « convert to kebab-case
;; plain: « convert to plain case
;; ]
;; returns: :string
;; example: {
;; recase.snake "This is an example"
;; ; => "this_is_an_example"
;;
;; recase.camel "Print SQL"
;; ; => "printSQL"
;; }
ret: str
unless null? attr 'title -> ret: \titleCase ret
unless null? attr 'camel -> ret: \camelCase ret
unless null? attr 'snake -> ret: \snakeCase ret
unless null? attr 'kebab -> ret: \kebabCase ret
unless null? attr 'plain -> ret: \plainCase ret
return ret
]
]!
;---------------------------
; Testing
;---------------------------
if standalone? ::
loop ["This is an example", "ThisIsAnExample", "this345_is_an_example", "printSQL", "ДоброЈутро"] 'str [
print ["String is:" str]
print ["Camel case:" recase.camel str]
print ["Title case:" recase.title str]
print ["Kebab case:" recase.kebab str]
print ["Snake case:" recase.snake str]
print ["Plain case:" recase.plain str]
print "----"
]