@pgpm/inflection 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/Makefile +6 -0
- package/README.md +5 -0
- package/__tests__/inflection.test.ts +181 -0
- package/deploy/schemas/inflection/procedures/camel.sql +25 -0
- package/deploy/schemas/inflection/procedures/dashed.sql +29 -0
- package/deploy/schemas/inflection/procedures/no_consecutive_caps.sql +53 -0
- package/deploy/schemas/inflection/procedures/no_single_underscores.sql +70 -0
- package/deploy/schemas/inflection/procedures/pascal.sql +20 -0
- package/deploy/schemas/inflection/procedures/pg_slugify.sql +68 -0
- package/deploy/schemas/inflection/procedures/plural.sql +31 -0
- package/deploy/schemas/inflection/procedures/should_skip_uncountable.sql +17 -0
- package/deploy/schemas/inflection/procedures/singular.sql +31 -0
- package/deploy/schemas/inflection/procedures/slugify.sql +59 -0
- package/deploy/schemas/inflection/procedures/uncountable_words.sql +14 -0
- package/deploy/schemas/inflection/procedures/underscore.sql +52 -0
- package/deploy/schemas/inflection/schema.sql +15 -0
- package/deploy/schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture.sql +129 -0
- package/deploy/schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx.sql +12 -0
- package/deploy/schemas/inflection/tables/inflection_rules/table.sql +16 -0
- package/jest.config.js +15 -0
- package/launchql-inflection.control +8 -0
- package/launchql.plan +20 -0
- package/package.json +28 -0
- package/revert/schemas/inflection/procedures/camel.sql +7 -0
- package/revert/schemas/inflection/procedures/dashed.sql +7 -0
- package/revert/schemas/inflection/procedures/no_consecutive_caps.sql +7 -0
- package/revert/schemas/inflection/procedures/no_single_underscores.sql +7 -0
- package/revert/schemas/inflection/procedures/pascal.sql +7 -0
- package/revert/schemas/inflection/procedures/pg_slugify.sql +8 -0
- package/revert/schemas/inflection/procedures/plural.sql +7 -0
- package/revert/schemas/inflection/procedures/should_skip_uncountable.sql +7 -0
- package/revert/schemas/inflection/procedures/singular.sql +7 -0
- package/revert/schemas/inflection/procedures/slugify.sql +8 -0
- package/revert/schemas/inflection/procedures/uncountable_words.sql +7 -0
- package/revert/schemas/inflection/procedures/underscore.sql +7 -0
- package/revert/schemas/inflection/schema.sql +7 -0
- package/revert/schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture.sql +5 -0
- package/revert/schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx.sql +7 -0
- package/revert/schemas/inflection/tables/inflection_rules/table.sql +7 -0
- package/sql/launchql-inflection--0.4.7.sql +451 -0
- package/verify/schemas/inflection/procedures/camel.sql +7 -0
- package/verify/schemas/inflection/procedures/dashed.sql +7 -0
- package/verify/schemas/inflection/procedures/no_consecutive_caps.sql +7 -0
- package/verify/schemas/inflection/procedures/no_single_underscores.sql +7 -0
- package/verify/schemas/inflection/procedures/pascal.sql +7 -0
- package/verify/schemas/inflection/procedures/pg_slugify.sql +7 -0
- package/verify/schemas/inflection/procedures/plural.sql +7 -0
- package/verify/schemas/inflection/procedures/should_skip_uncountable.sql +7 -0
- package/verify/schemas/inflection/procedures/singular.sql +7 -0
- package/verify/schemas/inflection/procedures/slugify.sql +7 -0
- package/verify/schemas/inflection/procedures/uncountable_words.sql +7 -0
- package/verify/schemas/inflection/procedures/underscore.sql +7 -0
- package/verify/schemas/inflection/schema.sql +7 -0
- package/verify/schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture.sql +5 -0
- package/verify/schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx.sql +7 -0
- package/verify/schemas/inflection/tables/inflection_rules/table.sql +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Interweb, Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/Makefile
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import cases from 'jest-in-case';
|
|
2
|
+
import { getConnections, PgTestClient } from 'pgsql-test';
|
|
3
|
+
|
|
4
|
+
let pg: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
describe('inflection', () => {
|
|
8
|
+
beforeAll(async () => {
|
|
9
|
+
({ pg, teardown } = await getConnections());
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterAll(async () => {
|
|
13
|
+
await teardown();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
cases(
|
|
17
|
+
'slugify',
|
|
18
|
+
async (opts: { name: string; allowUnicode: boolean; result: string }) => {
|
|
19
|
+
const { pg_slugify } = await pg.one(
|
|
20
|
+
'SELECT * FROM inflection.pg_slugify($1, $2)',
|
|
21
|
+
[opts.name, opts.allowUnicode]
|
|
22
|
+
);
|
|
23
|
+
expect(pg_slugify).toEqual(opts.result);
|
|
24
|
+
},
|
|
25
|
+
[
|
|
26
|
+
{ name: 'Hello, World!', allowUnicode: false, result: 'Hello_World' },
|
|
27
|
+
{ name: 'Héllø, Wørld!', allowUnicode: false, result: 'Hello_World' },
|
|
28
|
+
{ name: 'spam & eggs', allowUnicode: false, result: 'spam_eggs' },
|
|
29
|
+
{ name: 'spam & ıçüş', allowUnicode: true, result: 'spam_ıçüş' },
|
|
30
|
+
{ name: 'foo ıç bar', allowUnicode: true, result: 'foo_ıç_bar' },
|
|
31
|
+
{ name: ' foo ıç bar', allowUnicode: true, result: 'foo_ıç_bar' },
|
|
32
|
+
{ name: '你好', allowUnicode: true, result: '你好' },
|
|
33
|
+
{ name: 'message_properties', allowUnicode: false, result: 'message_properties' },
|
|
34
|
+
{ name: 'MessageProperties', allowUnicode: false, result: 'MessageProperties' },
|
|
35
|
+
{ name: 'WebACL', allowUnicode: false, result: 'WebAcl' }
|
|
36
|
+
]
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
cases(
|
|
40
|
+
'underscore',
|
|
41
|
+
async (opts: { name: string; result: string }) => {
|
|
42
|
+
const { underscore } = await pg.one(
|
|
43
|
+
'SELECT * FROM inflection.underscore($1)',
|
|
44
|
+
[opts.name]
|
|
45
|
+
);
|
|
46
|
+
expect(underscore).toEqual(opts.result);
|
|
47
|
+
},
|
|
48
|
+
[
|
|
49
|
+
{ name: 'MessageProperties', result: 'message_properties' },
|
|
50
|
+
{ name: 'messageProperties', result: 'message_properties' },
|
|
51
|
+
{ name: 'message_properties', result: 'message_properties' },
|
|
52
|
+
{ name: 'User Post', result: 'user_post' },
|
|
53
|
+
{ name: 'MP', result: 'mp' },
|
|
54
|
+
{ name: 'WebACL', result: 'web_acl' },
|
|
55
|
+
{ name: 'wabCdEfgh', result: 'wab_cd_efgh' },
|
|
56
|
+
{ name: 'WabCDEfgH', result: 'wab_cd_efgh' }
|
|
57
|
+
]
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
cases(
|
|
61
|
+
'no_single_underscores',
|
|
62
|
+
async (opts: { name: string; result: string }) => {
|
|
63
|
+
const { no_single_underscores } = await pg.one(
|
|
64
|
+
'SELECT * FROM inflection.no_single_underscores($1)',
|
|
65
|
+
[opts.name]
|
|
66
|
+
);
|
|
67
|
+
expect(no_single_underscores).toEqual(opts.result);
|
|
68
|
+
},
|
|
69
|
+
[
|
|
70
|
+
{ name: 'w_a_b_cd_efg_h', result: 'wab_cd_efgh' }
|
|
71
|
+
]
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
cases(
|
|
75
|
+
'pascal',
|
|
76
|
+
async (opts: { name: string; result: string }) => {
|
|
77
|
+
const { pascal } = await pg.one(
|
|
78
|
+
'SELECT * FROM inflection.pascal($1)',
|
|
79
|
+
[opts.name]
|
|
80
|
+
);
|
|
81
|
+
expect(pascal).toEqual(opts.result);
|
|
82
|
+
},
|
|
83
|
+
[
|
|
84
|
+
{ name: 'MessageProperties', result: 'MessageProperties' },
|
|
85
|
+
{ name: 'message_properties', result: 'MessageProperties' },
|
|
86
|
+
{ name: 'messageProperties', result: 'MessageProperties' },
|
|
87
|
+
{ name: 'MP', result: 'Mp' },
|
|
88
|
+
{ name: 'WebAcl', result: 'WebAcl' },
|
|
89
|
+
{ name: 'WebACL', result: 'WebAcl' },
|
|
90
|
+
{ name: 'web_acl', result: 'WebAcl' },
|
|
91
|
+
{ name: 'web acl', result: 'WebAcl' },
|
|
92
|
+
{ name: 'Web Acl', result: 'WebAcl' },
|
|
93
|
+
{ name: 'Web ACL', result: 'WebAcl' },
|
|
94
|
+
{ name: 'w_a_b', result: 'Wab' }
|
|
95
|
+
]
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
cases(
|
|
99
|
+
'camel',
|
|
100
|
+
async (opts: { name: string; result: string }) => {
|
|
101
|
+
const { camel } = await pg.one(
|
|
102
|
+
'SELECT * FROM inflection.camel($1)',
|
|
103
|
+
[opts.name]
|
|
104
|
+
);
|
|
105
|
+
expect(camel).toEqual(opts.result);
|
|
106
|
+
},
|
|
107
|
+
[
|
|
108
|
+
{ name: 'MessageProperties', result: 'messageProperties' },
|
|
109
|
+
{ name: 'message_properties', result: 'messageProperties' },
|
|
110
|
+
{ name: 'messageProperties', result: 'messageProperties' },
|
|
111
|
+
{ name: 'MP', result: 'mp' },
|
|
112
|
+
{ name: 'webAcl', result: 'webAcl' },
|
|
113
|
+
{ name: 'WebACL', result: 'webAcl' },
|
|
114
|
+
{ name: 'web_acl', result: 'webAcl' },
|
|
115
|
+
{ name: 'web acl', result: 'webAcl' },
|
|
116
|
+
{ name: 'Web Acl', result: 'webAcl' },
|
|
117
|
+
{ name: 'Web ACL', result: 'webAcl' },
|
|
118
|
+
{ name: 'w_a_b', result: 'wab' },
|
|
119
|
+
{ name: 'w_a_b_cd_efg_h', result: 'wabCdEfgh' }
|
|
120
|
+
]
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
cases(
|
|
124
|
+
'no_consecutive_caps',
|
|
125
|
+
async (opts: { name: string; result: string }) => {
|
|
126
|
+
const { no_consecutive_caps } = await pg.one(
|
|
127
|
+
'SELECT * FROM inflection.no_consecutive_caps($1)',
|
|
128
|
+
[opts.name]
|
|
129
|
+
);
|
|
130
|
+
expect(no_consecutive_caps).toEqual(opts.result);
|
|
131
|
+
},
|
|
132
|
+
[
|
|
133
|
+
{ name: 'MP', result: 'Mp' },
|
|
134
|
+
{ name: 'Web_ACL', result: 'Web_Acl' },
|
|
135
|
+
{ name: 'MPComplete', result: 'MpComplete' },
|
|
136
|
+
{ name: 'ACLWindow', result: 'AclWindow' }
|
|
137
|
+
]
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
cases(
|
|
141
|
+
'plural',
|
|
142
|
+
async (opts: { name: string; result: string }) => {
|
|
143
|
+
const { plural } = await pg.one(
|
|
144
|
+
'SELECT * FROM inflection.plural($1)',
|
|
145
|
+
[opts.name]
|
|
146
|
+
);
|
|
147
|
+
expect(plural).toEqual(opts.result);
|
|
148
|
+
},
|
|
149
|
+
[
|
|
150
|
+
{ name: 'user_login', result: 'user_logins' },
|
|
151
|
+
{ name: 'user Login', result: 'user Logins' },
|
|
152
|
+
{ name: 'user_logins', result: 'user_logins' },
|
|
153
|
+
{ name: 'user Logins', result: 'user Logins' },
|
|
154
|
+
{ name: 'children', result: 'children' },
|
|
155
|
+
{ name: 'child', result: 'children' },
|
|
156
|
+
{ name: 'man', result: 'men' },
|
|
157
|
+
{ name: 'men', result: 'men' }
|
|
158
|
+
]
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
cases(
|
|
162
|
+
'singular',
|
|
163
|
+
async (opts: { name: string; result: string }) => {
|
|
164
|
+
const { singular } = await pg.one(
|
|
165
|
+
'SELECT * FROM inflection.singular($1)',
|
|
166
|
+
[opts.name]
|
|
167
|
+
);
|
|
168
|
+
expect(singular).toEqual(opts.result);
|
|
169
|
+
},
|
|
170
|
+
[
|
|
171
|
+
{ name: 'user_logins', result: 'user_login' },
|
|
172
|
+
{ name: 'user Logins', result: 'user Login' },
|
|
173
|
+
{ name: 'user_login', result: 'user_login' },
|
|
174
|
+
{ name: 'user Login', result: 'user Login' },
|
|
175
|
+
{ name: 'children', result: 'child' },
|
|
176
|
+
{ name: 'child', result: 'child' },
|
|
177
|
+
{ name: 'man', result: 'man' },
|
|
178
|
+
{ name: 'men', result: 'man' }
|
|
179
|
+
]
|
|
180
|
+
);
|
|
181
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/camel to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
-- requires: schemas/inflection/procedures/underscore
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
CREATE FUNCTION inflection.camel (str text)
|
|
9
|
+
RETURNS text
|
|
10
|
+
AS $$
|
|
11
|
+
DECLARE
|
|
12
|
+
result text[];
|
|
13
|
+
BEGIN
|
|
14
|
+
str = inflection.underscore(str);
|
|
15
|
+
FOR result IN
|
|
16
|
+
SELECT regexp_matches(str, E'(_[a-zA-Z0-9])', 'g')
|
|
17
|
+
LOOP
|
|
18
|
+
str = replace(str, result[1], upper(result[1]));
|
|
19
|
+
END LOOP;
|
|
20
|
+
return regexp_replace(substring(str FROM 1 FOR 1) || substring(str FROM 2 FOR length(str)), E'[_]+', '', 'gi');
|
|
21
|
+
END;
|
|
22
|
+
$$
|
|
23
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
24
|
+
|
|
25
|
+
COMMIT;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/dashed to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
-- requires: schemas/inflection/procedures/underscore
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
CREATE FUNCTION inflection.dashed(str text)
|
|
9
|
+
RETURNS text
|
|
10
|
+
AS $$
|
|
11
|
+
WITH underscored AS (
|
|
12
|
+
SELECT
|
|
13
|
+
inflection.underscore(str) AS value
|
|
14
|
+
),
|
|
15
|
+
dashes AS (
|
|
16
|
+
SELECT
|
|
17
|
+
regexp_replace(value, '_', '-', 'gi') AS value
|
|
18
|
+
FROM
|
|
19
|
+
underscored
|
|
20
|
+
)
|
|
21
|
+
SELECT
|
|
22
|
+
value
|
|
23
|
+
FROM
|
|
24
|
+
dashes;
|
|
25
|
+
$$
|
|
26
|
+
LANGUAGE 'sql'
|
|
27
|
+
IMMUTABLE;
|
|
28
|
+
COMMIT;
|
|
29
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/no_consecutive_caps to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION inflection.no_consecutive_caps_till_end(
|
|
8
|
+
str text
|
|
9
|
+
) returns text as $$
|
|
10
|
+
DECLARE
|
|
11
|
+
result text[];
|
|
12
|
+
temp text;
|
|
13
|
+
BEGIN
|
|
14
|
+
FOR result IN
|
|
15
|
+
SELECT regexp_matches(str, E'([A-Z])([A-Z]+$)', 'g')
|
|
16
|
+
LOOP
|
|
17
|
+
temp = result[1] || lower(result[2]);
|
|
18
|
+
str = replace(str, result[1] || result[2], temp);
|
|
19
|
+
END LOOP;
|
|
20
|
+
return str;
|
|
21
|
+
END;
|
|
22
|
+
$$
|
|
23
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
CREATE FUNCTION inflection.no_consecutive_caps_till_lower(
|
|
27
|
+
str text
|
|
28
|
+
) returns text as $$
|
|
29
|
+
DECLARE
|
|
30
|
+
result text[];
|
|
31
|
+
temp text;
|
|
32
|
+
BEGIN
|
|
33
|
+
FOR result IN
|
|
34
|
+
SELECT regexp_matches(str, E'([A-Z])([A-Z]+)[A-Z][a-z]', 'g')
|
|
35
|
+
LOOP
|
|
36
|
+
temp = result[1] || lower(result[2]);
|
|
37
|
+
str = replace(str, result[1] || result[2], temp);
|
|
38
|
+
END LOOP;
|
|
39
|
+
|
|
40
|
+
return str;
|
|
41
|
+
END;
|
|
42
|
+
$$
|
|
43
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
CREATE FUNCTION inflection.no_consecutive_caps(
|
|
47
|
+
str text
|
|
48
|
+
) returns text as $$
|
|
49
|
+
select inflection.no_consecutive_caps_till_lower(inflection.no_consecutive_caps_till_end(str));
|
|
50
|
+
$$
|
|
51
|
+
LANGUAGE 'sql' STABLE;
|
|
52
|
+
|
|
53
|
+
COMMIT;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/no_single_underscores to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION inflection.no_single_underscores_in_beginning(
|
|
8
|
+
str text
|
|
9
|
+
) returns text as $$
|
|
10
|
+
DECLARE
|
|
11
|
+
result text[];
|
|
12
|
+
temp text;
|
|
13
|
+
BEGIN
|
|
14
|
+
FOR result IN
|
|
15
|
+
SELECT regexp_matches(str, E'(^[a-z])(_)', 'g')
|
|
16
|
+
LOOP
|
|
17
|
+
str = replace(str, result[1] || result[2], result[1]);
|
|
18
|
+
END LOOP;
|
|
19
|
+
return str;
|
|
20
|
+
END;
|
|
21
|
+
$$
|
|
22
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
CREATE FUNCTION inflection.no_single_underscores_at_end(
|
|
26
|
+
str text
|
|
27
|
+
) returns text as $$
|
|
28
|
+
DECLARE
|
|
29
|
+
result text[];
|
|
30
|
+
temp text;
|
|
31
|
+
BEGIN
|
|
32
|
+
FOR result IN
|
|
33
|
+
SELECT regexp_matches(str, E'(_)([a-z]$)', 'g')
|
|
34
|
+
LOOP
|
|
35
|
+
str = replace(str, result[1] || result[2], result[2]);
|
|
36
|
+
END LOOP;
|
|
37
|
+
|
|
38
|
+
return str;
|
|
39
|
+
END;
|
|
40
|
+
$$
|
|
41
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
42
|
+
|
|
43
|
+
CREATE FUNCTION inflection.no_single_underscores_in_middle(
|
|
44
|
+
str text
|
|
45
|
+
) returns text as $$
|
|
46
|
+
DECLARE
|
|
47
|
+
result text[];
|
|
48
|
+
temp text;
|
|
49
|
+
BEGIN
|
|
50
|
+
FOR result IN
|
|
51
|
+
SELECT regexp_matches(str, E'(_)([a-z]_)', 'g')
|
|
52
|
+
LOOP
|
|
53
|
+
str = replace(str, result[1] || result[2], result[2]);
|
|
54
|
+
END LOOP;
|
|
55
|
+
|
|
56
|
+
return str;
|
|
57
|
+
END;
|
|
58
|
+
$$
|
|
59
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
CREATE FUNCTION inflection.no_single_underscores(
|
|
63
|
+
str text
|
|
64
|
+
) returns text as $$
|
|
65
|
+
select
|
|
66
|
+
inflection.no_single_underscores_in_middle(inflection.no_single_underscores_at_end(inflection.no_single_underscores_in_beginning(str)));
|
|
67
|
+
$$
|
|
68
|
+
LANGUAGE 'sql' STABLE;
|
|
69
|
+
|
|
70
|
+
COMMIT;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/pascal to pg
|
|
2
|
+
-- requires: schemas/inflection/schema
|
|
3
|
+
-- requires: schemas/inflection/procedures/camel
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION inflection.pascal (str text)
|
|
8
|
+
RETURNS text
|
|
9
|
+
AS $$
|
|
10
|
+
DECLARE
|
|
11
|
+
result text[];
|
|
12
|
+
BEGIN
|
|
13
|
+
str = inflection.camel(str);
|
|
14
|
+
return upper(substring(str FROM 1 FOR 1)) || substring(str FROM 2 FOR length(str));
|
|
15
|
+
END;
|
|
16
|
+
$$
|
|
17
|
+
LANGUAGE 'plpgsql' STABLE;
|
|
18
|
+
|
|
19
|
+
COMMIT;
|
|
20
|
+
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/pg_slugify to pg
|
|
2
|
+
-- requires: schemas/inflection/schema
|
|
3
|
+
-- requires: schemas/inflection/procedures/no_consecutive_caps
|
|
4
|
+
-- NOTE: this does NOT lowercase, and uses underscores instead of dashes
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
CREATE FUNCTION inflection.pg_slugify (value text, allow_unicode boolean)
|
|
8
|
+
RETURNS text
|
|
9
|
+
AS $$
|
|
10
|
+
WITH normalized AS (
|
|
11
|
+
SELECT
|
|
12
|
+
CASE WHEN allow_unicode THEN
|
|
13
|
+
value
|
|
14
|
+
ELSE
|
|
15
|
+
unaccent (value)
|
|
16
|
+
END AS value
|
|
17
|
+
),
|
|
18
|
+
no_consecutive_caps AS (
|
|
19
|
+
SELECT
|
|
20
|
+
inflection.no_consecutive_caps (value) AS value
|
|
21
|
+
FROM
|
|
22
|
+
normalized
|
|
23
|
+
),
|
|
24
|
+
remove_chars AS (
|
|
25
|
+
SELECT
|
|
26
|
+
regexp_replace(value, E'[^\\w\\s-]', '', 'gi') AS value
|
|
27
|
+
FROM
|
|
28
|
+
no_consecutive_caps
|
|
29
|
+
),
|
|
30
|
+
trimmed AS (
|
|
31
|
+
SELECT
|
|
32
|
+
trim(value) AS value
|
|
33
|
+
FROM
|
|
34
|
+
remove_chars
|
|
35
|
+
),
|
|
36
|
+
hyphenated AS (
|
|
37
|
+
SELECT
|
|
38
|
+
regexp_replace(value, E'[-\\s]+', '-', 'gi') AS value
|
|
39
|
+
FROM
|
|
40
|
+
trimmed
|
|
41
|
+
),
|
|
42
|
+
underscored AS (
|
|
43
|
+
SELECT
|
|
44
|
+
regexp_replace(value, E'[-]+', '_', 'gi') AS value
|
|
45
|
+
FROM
|
|
46
|
+
hyphenated
|
|
47
|
+
),
|
|
48
|
+
removedups AS (
|
|
49
|
+
SELECT
|
|
50
|
+
regexp_replace(value, E'[_]+', '_', 'gi') AS value
|
|
51
|
+
FROM
|
|
52
|
+
underscored
|
|
53
|
+
)
|
|
54
|
+
SELECT
|
|
55
|
+
value
|
|
56
|
+
FROM
|
|
57
|
+
removedups;
|
|
58
|
+
$$
|
|
59
|
+
LANGUAGE SQL
|
|
60
|
+
STRICT IMMUTABLE;
|
|
61
|
+
-- default false overload
|
|
62
|
+
CREATE FUNCTION inflection.pg_slugify (text)
|
|
63
|
+
RETURNS text
|
|
64
|
+
AS 'SELECT inflection.pg_slugify($1, false)'
|
|
65
|
+
LANGUAGE SQL
|
|
66
|
+
IMMUTABLE;
|
|
67
|
+
COMMIT;
|
|
68
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/plural to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
-- requires: schemas/inflection/tables/inflection_rules/table
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
CREATE FUNCTION inflection.plural (str text)
|
|
9
|
+
RETURNS text
|
|
10
|
+
AS $$
|
|
11
|
+
DECLARE
|
|
12
|
+
result record;
|
|
13
|
+
matches text[];
|
|
14
|
+
BEGIN
|
|
15
|
+
FOR result IN
|
|
16
|
+
SELECT * FROM inflection.inflection_rules where type='plural'
|
|
17
|
+
LOOP
|
|
18
|
+
matches = regexp_matches(str, result.test, 'gi');
|
|
19
|
+
IF (array_length(matches, 1) > 0) THEN
|
|
20
|
+
IF (result.replacement IS NULL) THEN
|
|
21
|
+
return str;
|
|
22
|
+
END IF;
|
|
23
|
+
str = regexp_replace(str, result.test, result.replacement, 'gi');
|
|
24
|
+
return str;
|
|
25
|
+
END IF;
|
|
26
|
+
END LOOP;
|
|
27
|
+
return str;
|
|
28
|
+
END;
|
|
29
|
+
$$
|
|
30
|
+
LANGUAGE 'plpgsql' IMMUTABLE;
|
|
31
|
+
COMMIT;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/should_skip_uncountable to pg
|
|
2
|
+
-- requires: schemas/inflection/schema
|
|
3
|
+
-- requires: schemas/inflection/procedures/uncountable_words
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION inflection.should_skip_uncountable (str text)
|
|
8
|
+
RETURNS boolean
|
|
9
|
+
AS $$
|
|
10
|
+
SELECT
|
|
11
|
+
str = ANY (inflection.uncountable_words ());
|
|
12
|
+
$$
|
|
13
|
+
LANGUAGE 'sql'
|
|
14
|
+
IMMUTABLE;
|
|
15
|
+
|
|
16
|
+
COMMIT;
|
|
17
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/singular to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
-- requires: schemas/inflection/tables/inflection_rules/table
|
|
5
|
+
|
|
6
|
+
BEGIN;
|
|
7
|
+
|
|
8
|
+
CREATE FUNCTION inflection.singular (str text)
|
|
9
|
+
RETURNS text
|
|
10
|
+
AS $$
|
|
11
|
+
DECLARE
|
|
12
|
+
result record;
|
|
13
|
+
matches text[];
|
|
14
|
+
BEGIN
|
|
15
|
+
FOR result IN
|
|
16
|
+
SELECT * FROM inflection.inflection_rules where type='singular'
|
|
17
|
+
LOOP
|
|
18
|
+
matches = regexp_matches(str, result.test, 'gi');
|
|
19
|
+
IF (array_length(matches, 1) > 0) THEN
|
|
20
|
+
IF (result.replacement IS NULL) THEN
|
|
21
|
+
return str;
|
|
22
|
+
END IF;
|
|
23
|
+
str = regexp_replace(str, result.test, result.replacement, 'gi');
|
|
24
|
+
return str;
|
|
25
|
+
END IF;
|
|
26
|
+
END LOOP;
|
|
27
|
+
return str;
|
|
28
|
+
END;
|
|
29
|
+
$$
|
|
30
|
+
LANGUAGE 'plpgsql' IMMUTABLE;
|
|
31
|
+
COMMIT;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/slugify to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
|
|
5
|
+
-- https://schinckel.net/2015/12/16/slugify()-for-postgres-(almost)/
|
|
6
|
+
|
|
7
|
+
BEGIN;
|
|
8
|
+
CREATE FUNCTION inflection.slugify (value text, allow_unicode boolean)
|
|
9
|
+
RETURNS text
|
|
10
|
+
AS $$
|
|
11
|
+
WITH normalized AS (
|
|
12
|
+
SELECT
|
|
13
|
+
CASE WHEN allow_unicode THEN
|
|
14
|
+
value
|
|
15
|
+
ELSE
|
|
16
|
+
unaccent (value)
|
|
17
|
+
END AS value
|
|
18
|
+
),
|
|
19
|
+
remove_chars AS (
|
|
20
|
+
SELECT
|
|
21
|
+
regexp_replace(value, E'[^\\w\\s-]', '', 'gi') AS value
|
|
22
|
+
FROM
|
|
23
|
+
normalized
|
|
24
|
+
),
|
|
25
|
+
lowercase AS (
|
|
26
|
+
SELECT
|
|
27
|
+
lower(value) AS value
|
|
28
|
+
FROM
|
|
29
|
+
remove_chars
|
|
30
|
+
),
|
|
31
|
+
trimmed AS (
|
|
32
|
+
SELECT
|
|
33
|
+
trim(value) AS value
|
|
34
|
+
FROM
|
|
35
|
+
lowercase
|
|
36
|
+
),
|
|
37
|
+
hyphenated AS (
|
|
38
|
+
SELECT
|
|
39
|
+
regexp_replace(value, E'[-\\s]+', '-', 'gi') AS value
|
|
40
|
+
FROM
|
|
41
|
+
trimmed
|
|
42
|
+
)
|
|
43
|
+
SELECT
|
|
44
|
+
value
|
|
45
|
+
FROM
|
|
46
|
+
hyphenated;
|
|
47
|
+
$$
|
|
48
|
+
LANGUAGE SQL
|
|
49
|
+
STRICT IMMUTABLE;
|
|
50
|
+
|
|
51
|
+
-- default false overload
|
|
52
|
+
CREATE FUNCTION inflection.slugify (text)
|
|
53
|
+
RETURNS text
|
|
54
|
+
AS 'SELECT inflection.slugify($1, false)'
|
|
55
|
+
LANGUAGE SQL
|
|
56
|
+
IMMUTABLE;
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
COMMIT;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
-- Deploy schemas/inflection/procedures/uncountable_words to pg
|
|
2
|
+
|
|
3
|
+
-- requires: schemas/inflection/schema
|
|
4
|
+
|
|
5
|
+
BEGIN;
|
|
6
|
+
|
|
7
|
+
CREATE FUNCTION inflection.uncountable_words ()
|
|
8
|
+
RETURNS text[]
|
|
9
|
+
AS $$
|
|
10
|
+
select ARRAY[ 'accommodation', 'adulthood', 'advertising', 'advice', 'aggression', 'aid', 'air', 'aircraft', 'alcohol', 'anger', 'applause', 'arithmetic', 'assistance', 'athletics', 'bacon', 'baggage', 'beef', 'biology', 'blood', 'botany', 'bread', 'butter', 'carbon', 'cardboard', 'cash', 'chalk', 'chaos', 'chess', 'crossroads', 'countryside', 'dancing', 'deer', 'dignity', 'dirt', 'dust', 'economics', 'education', 'electricity', 'engineering', 'enjoyment', 'envy', 'equipment', 'ethics', 'evidence', 'evolution', 'fame', 'fiction', 'flour', 'flu', 'food', 'fuel', 'fun', 'furniture', 'gallows', 'garbage', 'garlic', 'genetics', 'gold', 'golf', 'gossip', 'grammar', 'gratitude', 'grief', 'guilt', 'gymnastics', 'happiness', 'hardware', 'harm', 'hate', 'hatred', 'health', 'heat', 'help', 'homework', 'honesty', 'honey', 'hospitality', 'housework', 'humour', 'hunger', 'hydrogen', 'ice', 'importance', 'inflation', 'information', 'innocence', 'iron', 'irony', 'jam', 'jewelry', 'judo', 'karate', 'knowledge', 'lack', 'laughter', 'lava', 'leather', 'leisure', 'lightning', 'linguine', 'linguini', 'linguistics', 'literature', 'litter', 'livestock', 'logic', 'loneliness', 'luck', 'luggage', 'macaroni', 'machinery', 'magic', 'management', 'mankind', 'marble', 'mathematics', 'mayonnaise', 'measles', 'methane', 'milk', 'minus', 'money', 'mud', 'music', 'mumps', 'nature', 'news', 'nitrogen', 'nonsense', 'nurture', 'nutrition', 'obedience', 'obesity', 'oxygen', 'pasta', 'patience', 'physics', 'poetry', 'pollution', 'poverty', 'pride', 'psychology', 'publicity', 'punctuation', 'quartz', 'racism', 'relaxation', 'reliability', 'research', 'respect', 'revenge', 'rice', 'rubbish', 'rum', 'safety', 'scenery', 'seafood', 'seaside', 'series', 'shame', 'sheep', 'shopping', 'sleep', 'smoke', 'smoking', 'snow', 'soap', 'software', 'soil', 'spaghetti', 'species', 'steam', 'stuff', 'stupidity', 'sunshine', 'symmetry', 'tennis', 'thirst', 'thunder', 'timber', 'traffic', 'transportation', 'trust', 'underwear', 'unemployment', 'unity', 'validity', 'veal', 'vegetation', 'vegetarianism', 'vengeance', 'violence', 'vitality', 'warmth', 'wealth', 'weather', 'welfare', 'wheat', 'wildlife', 'wisdom', 'yoga', 'zinc', 'zoology' ];
|
|
11
|
+
$$
|
|
12
|
+
LANGUAGE 'sql' IMMUTABLE;
|
|
13
|
+
|
|
14
|
+
COMMIT;
|