Skip to content

Commit a3350d3

Browse files
committed
underscores issue fixed for now
1 parent 468d763 commit a3350d3

File tree

8 files changed

+215
-8
lines changed

8 files changed

+215
-8
lines changed

packages/inflection/deploy/schemas/inflection/procedures/no_consecutive_caps.sql

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,34 @@
44

55
BEGIN;
66

7-
CREATE FUNCTION inflection.no_consecutive_caps(
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(
827
str text
928
) returns text as $$
1029
DECLARE
1130
result text[];
1231
temp text;
1332
BEGIN
1433
FOR result IN
15-
SELECT regexp_matches(str, E'([A-Z])([A-Z]+)', 'g')
34+
SELECT regexp_matches(str, E'([A-Z])([A-Z]+)[A-Z][a-z]', 'g')
1635
LOOP
1736
temp = result[1] || lower(result[2]);
1837
str = replace(str, result[1] || result[2], temp);
@@ -23,4 +42,12 @@ END;
2342
$$
2443
LANGUAGE 'plpgsql' STABLE;
2544

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+
2653
COMMIT;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

packages/inflection/deploy/schemas/inflection/procedures/underscore.sql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- Deploy schemas/inflection/procedures/underscore to pg
22
-- requires: schemas/inflection/schema
33
-- requires: schemas/inflection/procedures/pg_slugify
4+
-- requires: schemas/inflection/procedures/no_single_underscores
45

56
BEGIN;
67
CREATE FUNCTION inflection.underscore (str text)
@@ -33,11 +34,17 @@ stripedges AS (
3334
regexp_replace(regexp_replace(value, E'([A-Z])_$', E'\\1', 'gi'), E'^_([A-Z])', E'\\1', 'gi') AS value
3435
FROM
3536
removedups
37+
),
38+
nosingles AS (
39+
SELECT
40+
inflection.no_single_underscores(value) AS value
41+
FROM
42+
stripedges
3643
)
3744
SELECT
3845
value
3946
FROM
40-
stripedges;
47+
nosingles;
4148
$$
4249
LANGUAGE 'sql'
4350
IMMUTABLE;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert schemas/inflection/procedures/no_single_underscores from pg
2+
3+
BEGIN;
4+
5+
DROP FUNCTION inflection.no_single_underscores;
6+
7+
COMMIT;

packages/inflection/sqitch.plan

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ schemas/inflection/procedures/should_skip_uncountable [schemas/inflection/schema
1616
schemas/inflection/procedures/singular [schemas/inflection/schema schemas/inflection/tables/inflection_rules/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/procedures/singular
1717
schemas/inflection/procedures/slugify [schemas/inflection/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/procedures/slugify
1818
schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture [schemas/inflection/schema schemas/inflection/tables/inflection_rules/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture
19-
schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx [schemas/inflection/schema schemas/inflection/tables/inflection_rules/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx
19+
schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx [schemas/inflection/schema schemas/inflection/tables/inflection_rules/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx
20+
schemas/inflection/procedures/no_single_underscores [schemas/inflection/schema] 2020-05-19T20:49:46Z Dan Lynch <[email protected]> # add schemas/inflection/procedures/no_single_underscores

packages/inflection/sql/inflection--0.0.1.sql

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
\echo Use "CREATE EXTENSION inflection" to load this file. \quit
22
CREATE SCHEMA inflection;
33

4-
CREATE FUNCTION inflection.no_consecutive_caps ( str text ) RETURNS text AS $EOFCODE$
4+
CREATE FUNCTION inflection.no_consecutive_caps_till_end ( str text ) RETURNS text AS $EOFCODE$
5+
DECLARE
6+
result text[];
7+
temp text;
8+
BEGIN
9+
FOR result IN
10+
SELECT regexp_matches(str, E'([A-Z])([A-Z]+$)', 'g')
11+
LOOP
12+
temp = result[1] || lower(result[2]);
13+
str = replace(str, result[1] || result[2], temp);
14+
END LOOP;
15+
return str;
16+
END;
17+
$EOFCODE$ LANGUAGE plpgsql STABLE;
18+
19+
CREATE FUNCTION inflection.no_consecutive_caps_till_lower ( str text ) RETURNS text AS $EOFCODE$
520
DECLARE
621
result text[];
722
temp text;
823
BEGIN
924
FOR result IN
10-
SELECT regexp_matches(str, E'([A-Z])([A-Z]+)', 'g')
25+
SELECT regexp_matches(str, E'([A-Z])([A-Z]+)[A-Z][a-z]', 'g')
1126
LOOP
1227
temp = result[1] || lower(result[2]);
1328
str = replace(str, result[1] || result[2], temp);
@@ -17,6 +32,10 @@ BEGIN
1732
END;
1833
$EOFCODE$ LANGUAGE plpgsql STABLE;
1934

35+
CREATE FUNCTION inflection.no_consecutive_caps ( str text ) RETURNS text AS $EOFCODE$
36+
select inflection.no_consecutive_caps_till_lower(inflection.no_consecutive_caps_till_end(str));
37+
$EOFCODE$ LANGUAGE sql STABLE;
38+
2039
CREATE FUNCTION inflection.pg_slugify ( value text, allow_unicode boolean ) RETURNS text AS $EOFCODE$
2140
WITH normalized AS (
2241
SELECT
@@ -70,6 +89,55 @@ $EOFCODE$ LANGUAGE sql STRICT IMMUTABLE;
7089

7190
CREATE FUNCTION inflection.pg_slugify ( text ) RETURNS text AS $EOFCODE$SELECT inflection.pg_slugify($1, false)$EOFCODE$ LANGUAGE sql IMMUTABLE STRICT;
7291

92+
CREATE FUNCTION inflection.no_single_underscores_in_beginning ( str text ) RETURNS text AS $EOFCODE$
93+
DECLARE
94+
result text[];
95+
temp text;
96+
BEGIN
97+
FOR result IN
98+
SELECT regexp_matches(str, E'(^[a-z])(_)', 'g')
99+
LOOP
100+
str = replace(str, result[1] || result[2], result[1]);
101+
END LOOP;
102+
return str;
103+
END;
104+
$EOFCODE$ LANGUAGE plpgsql STABLE;
105+
106+
CREATE FUNCTION inflection.no_single_underscores_at_end ( str text ) RETURNS text AS $EOFCODE$
107+
DECLARE
108+
result text[];
109+
temp text;
110+
BEGIN
111+
FOR result IN
112+
SELECT regexp_matches(str, E'(_)([a-z]$)', 'g')
113+
LOOP
114+
str = replace(str, result[1] || result[2], result[2]);
115+
END LOOP;
116+
117+
return str;
118+
END;
119+
$EOFCODE$ LANGUAGE plpgsql STABLE;
120+
121+
CREATE FUNCTION inflection.no_single_underscores_in_middle ( str text ) RETURNS text AS $EOFCODE$
122+
DECLARE
123+
result text[];
124+
temp text;
125+
BEGIN
126+
FOR result IN
127+
SELECT regexp_matches(str, E'(_)([a-z]_)', 'g')
128+
LOOP
129+
str = replace(str, result[1] || result[2], result[2]);
130+
END LOOP;
131+
132+
return str;
133+
END;
134+
$EOFCODE$ LANGUAGE plpgsql STABLE;
135+
136+
CREATE FUNCTION inflection.no_single_underscores ( str text ) RETURNS text AS $EOFCODE$
137+
select
138+
inflection.no_single_underscores_in_middle(inflection.no_single_underscores_at_end(inflection.no_single_underscores_in_beginning(str)));
139+
$EOFCODE$ LANGUAGE sql STABLE;
140+
73141
CREATE FUNCTION inflection.underscore ( str text ) RETURNS text AS $EOFCODE$
74142
WITH slugged AS (
75143
SELECT
@@ -98,11 +166,17 @@ stripedges AS (
98166
regexp_replace(regexp_replace(value, E'([A-Z])_$', E'\\1', 'gi'), E'^_([A-Z])', E'\\1', 'gi') AS value
99167
FROM
100168
removedups
169+
),
170+
nosingles AS (
171+
SELECT
172+
inflection.no_single_underscores(value) AS value
173+
FROM
174+
stripedges
101175
)
102176
SELECT
103177
value
104178
FROM
105-
stripedges;
179+
nosingles;
106180
$EOFCODE$ LANGUAGE sql IMMUTABLE;
107181

108182
CREATE FUNCTION inflection.camel ( str text ) RETURNS text AS $EOFCODE$

packages/inflection/test/__tests__/inflection/inflection.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ describe('inflection', () => {
4343
{ name: 'message_properties', result: 'message_properties' },
4444
{ name: 'User Post', result: 'user_post' },
4545
{ name: 'MP', result: 'mp' },
46-
{ name: 'WebACL', result: 'web_acl' },
46+
{ name: 'WebACL', result: 'web_acl' }
47+
]);
48+
cases('no_single_underscores', async opts => {
49+
const { no_single_underscores } = await db.one(
50+
'SELECT * FROM inflection.no_single_underscores( $1 )',
51+
[opts.name]
52+
);
53+
expect(no_single_underscores).toEqual(opts.result);
54+
}, [
55+
{ name: 'w_a_b_cd_efg_h', result: 'wab_cd_efgh' }
4756
]);
4857
cases('pascal', async opts => {
4958
const { pascal } = await db.one(
@@ -62,6 +71,7 @@ describe('inflection', () => {
6271
{ name: 'web acl', result: 'WebAcl' },
6372
{ name: 'Web Acl', result: 'WebAcl' },
6473
{ name: 'Web ACL', result: 'WebAcl' },
74+
{ name: 'w_a_b', result: 'Wab' },
6575
]);
6676
cases('camel', async opts => {
6777
const { camel } = await db.one(
@@ -80,6 +90,8 @@ describe('inflection', () => {
8090
{ name: 'web acl', result: 'webAcl' },
8191
{ name: 'Web Acl', result: 'webAcl' },
8292
{ name: 'Web ACL', result: 'webAcl' },
93+
{ name: 'w_a_b', result: 'wab' },
94+
{ name: 'w_a_b_cd_efg_h', result: 'wabCdEfgh' },
8395
]);
8496
cases('no_consecutive_caps', async opts => {
8597
const { no_consecutive_caps } = await db.one(
@@ -90,6 +102,8 @@ describe('inflection', () => {
90102
}, [
91103
{ name: 'MP', result: 'Mp' },
92104
{ name: 'Web_ACL', result: 'Web_Acl' },
105+
{ name: 'MPComplete', result: 'MpComplete' },
106+
{ name: 'ACLWindow', result: 'AclWindow' },
93107
]);
94108
cases('plural', async opts => {
95109
const { plural } = await db.one(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Verify schemas/inflection/procedures/no_single_underscores on pg
2+
3+
BEGIN;
4+
5+
SELECT verify_function ('inflection.no_single_underscores');
6+
7+
ROLLBACK;

0 commit comments

Comments
 (0)