Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
to
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
matrix
to
Commits
6325ea83
Commit
6325ea83
authored
5 years ago
by
Jorik Schellekens
Browse files
Options
Downloads
Patches
Plain Diff
Create link parser and formatter
parent
8b07e64a
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/parser/parser.test.ts
+82
-0
82 additions, 0 deletions
src/parser/parser.test.ts
src/parser/parser.ts
+175
-0
175 additions, 0 deletions
src/parser/parser.ts
src/parser/types.ts
+55
-0
55 additions, 0 deletions
src/parser/types.ts
with
312 additions
and
0 deletions
src/parser/parser.test.ts
0 → 100644
+
82
−
0
View file @
6325ea83
import
{
parseLink
,
parsePermalink
,
parseArgs
,
verifiers
,
discriminate
,
toURI
,
}
from
"
./parser
"
;
import
{
LinkDiscriminator
}
from
"
./types
"
;
const
curriedDiscriminate
=
(
id
:
string
)
=>
discriminate
(
id
,
verifiers
,
LinkDiscriminator
.
ParseFailed
);
it
(
"
types identifiers correctly
"
,
()
=>
{
expect
(
curriedDiscriminate
(
"
@user:matrix.org
"
)).
toEqual
(
LinkDiscriminator
.
UserId
);
expect
(
curriedDiscriminate
(
"
!room:matrix.org
"
)).
toEqual
(
LinkDiscriminator
.
RoomId
);
expect
(
curriedDiscriminate
(
"
!somewhere:example.org/$event:example.org
"
)
).
toEqual
(
LinkDiscriminator
.
Permalink
);
expect
(
curriedDiscriminate
(
"
+group:matrix.org
"
)).
toEqual
(
LinkDiscriminator
.
GroupId
);
expect
(
curriedDiscriminate
(
"
#alias:matrix.org
"
)).
toEqual
(
LinkDiscriminator
.
Alias
);
});
it
(
"
types garbadge as such
"
,
()
=>
{
expect
(
curriedDiscriminate
(
"
sdfa;fdlkja
"
)).
toEqual
(
LinkDiscriminator
.
ParseFailed
);
expect
(
curriedDiscriminate
(
"
$event$matrix.org
"
)).
toEqual
(
LinkDiscriminator
.
ParseFailed
);
expect
(
curriedDiscriminate
(
"
/user:matrix.org
"
)).
toEqual
(
LinkDiscriminator
.
ParseFailed
);
});
it
(
"
parses vias
"
,
()
=>
{
expect
(
parseArgs
(
"
via=example.org&via=alt.example.org
"
)
).
toHaveProperty
(
"
vias
"
,
[
"
example.org
"
,
"
alt.example.org
"
]);
});
it
(
"
parses sharer
"
,
()
=>
{
expect
(
parseArgs
(
"
sharer=blah
"
)).
toHaveProperty
(
"
sharer
"
,
"
blah
"
);
});
it
(
"
parses random args
"
,
()
=>
{
expect
(
parseArgs
(
"
via=qreqrqwer&banter=2342
"
)).
toHaveProperty
(
"
extras.banter
"
,
[
"
2342
"
]
);
});
it
(
"
parses permalinks
"
,
()
=>
{
expect
(
parsePermalink
(
"
!somewhere:example.org/$event:example.org
"
)).
toEqual
({
roomKind
:
LinkDiscriminator
.
RoomId
,
roomLink
:
"
!somewhere:example.org
"
,
eventId
:
"
$event:example.org
"
,
});
});
it
(
"
formats links correctly
"
,
()
=>
{
const
bigLink
=
"
!somewhere:example.org/$event:example.org?via=dfasdf&via=jfjafjaf&uselesstag=useless
"
;
const
host
=
"
matrix.org
"
;
const
prefix
=
host
+
"
/#/
"
;
const
parse
=
parseLink
(
bigLink
);
switch
(
parse
.
kind
)
{
case
LinkDiscriminator
.
ParseFailed
:
fail
(
"
Parse failed
"
);
default
:
expect
(
toURI
(
host
,
parse
)).
toEqual
(
prefix
+
bigLink
);
}
});
This diff is collapsed.
Click to expand it.
src/parser/parser.ts
0 → 100644
+
175
−
0
View file @
6325ea83
import
_
from
"
lodash
"
;
import
{
LinkDiscriminator
,
SafeLink
,
Link
,
LinkContent
,
Arguments
,
}
from
"
./types
"
;
/*
* Verifiers are regexes which will match valid
* identifiers to their type
*/
type
Verifier
<
A
>
=
[
RegExp
,
A
];
export
const
roomVerifiers
:
Verifier
<
LinkDiscriminator
.
Alias
|
LinkDiscriminator
.
RoomId
>
[]
=
[
[
/^#
([^\/
:
]
+
?)
:
(
.+
)
$/
,
LinkDiscriminator
.
Alias
],
[
/^!
([^\/
:
]
+
?)
:
(
.+
)
$/
,
LinkDiscriminator
.
RoomId
],
];
export
const
verifiers
:
Verifier
<
LinkDiscriminator
>
[]
=
[
[
/^
[\!
#
]([^\/
:
]
+
?)
:
(
.+
?)\/\$([^\/
:
]
+
?)
:
(
.+
?)
$/
,
LinkDiscriminator
.
Permalink
],
[
/^@
([^\/
:
]
+
?)
:
(
.+
)
$/
,
LinkDiscriminator
.
UserId
],
[
/^
\+([^\/
:
]
+
?)
:
(
.+
)
$/
,
LinkDiscriminator
.
GroupId
],
...
roomVerifiers
,
];
/*
* parseLink takes a striped hash link (without the '#/' prefix)
* and parses into a Link. If the parse failed the result will
* be ParseFailed
*/
export
function
parseLink
(
link
:
string
):
Link
{
const
[
identifier
,
args
]
=
link
.
split
(
"
?
"
);
const
kind
=
discriminate
(
identifier
,
verifiers
,
LinkDiscriminator
.
ParseFailed
);
const
{
vias
,
sharer
,
extras
}
=
parseArgs
(
args
);
let
parsedLink
:
LinkContent
=
{
identifier
,
arguments
:
{
vias
,
sharer
,
extras
,
},
originalLink
:
link
,
};
if
(
kind
===
LinkDiscriminator
.
Permalink
)
{
const
{
roomKind
,
roomLink
,
eventId
}
=
parsePermalink
(
identifier
);
return
{
kind
,
...
parsedLink
,
roomKind
,
roomLink
,
eventId
,
};
}
return
{
kind
,
...
parsedLink
,
};
}
/*
* Parses a permalink.
* Assumes the permalink is correct.
*/
export
function
parsePermalink
(
identifier
:
string
)
{
const
[
roomLink
,
eventId
]
=
identifier
.
split
(
"
/
"
);
const
roomKind
=
discriminate
(
roomLink
,
roomVerifiers
,
// This is hacky but we're assuming identifier is a valid permalink
LinkDiscriminator
.
Alias
);
return
{
roomKind
,
roomLink
,
eventId
,
};
}
/*
* descriminate applies the verifiers to the identifier and
* returns it's type
*/
export
function
discriminate
<
T
,
F
>
(
identifier
:
string
,
verifiers
:
Verifier
<
T
>
[],
fail
:
F
):
T
|
F
{
if
(
identifier
!==
encodeURI
(
identifier
))
{
return
fail
;
}
return
verifiers
.
reduce
<
T
|
F
>
((
discriminator
,
verifier
)
=>
{
if
(
discriminator
!==
fail
)
{
return
discriminator
;
}
if
(
identifier
.
match
(
verifier
[
0
]))
{
return
verifier
[
1
];
}
return
discriminator
;
},
fail
);
}
/*
* parseArgs parses the <extra args> part of matrix.to links
*/
export
function
parseArgs
(
args
:
string
):
Arguments
{
const
parsedArgTuples
=
_
.
groupBy
(
args
.
split
(
"
&
"
)
.
map
((
x
)
=>
x
.
split
(
"
=
"
))
.
filter
((
x
)
=>
x
.
length
==
2
),
(
arg
)
=>
{
return
arg
[
0
];
}
);
const
parsedArgs
=
_
.
mapValues
(
parsedArgTuples
,
(
arg
)
=>
arg
.
map
((
x
)
=>
x
[
1
])
);
const
{
via
,
sharer
,
...
extras
}
=
parsedArgs
;
return
{
vias
:
via
,
sharer
:
(
parsedArgs
.
sharer
||
[
undefined
])[
0
],
extras
,
};
}
/*
* toURI converts a parsed link to uri. Typically it's recommended
* to show the original link if it existed but this is handy in the
* case where this was constructed.
*/
export
function
toURI
(
hostname
:
string
,
link
:
SafeLink
):
string
{
const
cleanHostname
=
hostname
.
trim
().
replace
(
/
\/
+$/
,
""
);
switch
(
link
.
kind
)
{
case
LinkDiscriminator
.
GroupId
:
case
LinkDiscriminator
.
UserId
:
case
LinkDiscriminator
.
RoomId
:
case
LinkDiscriminator
.
Alias
:
case
LinkDiscriminator
.
Permalink
:
const
uri
=
encodeURI
(
cleanHostname
+
"
/#/
"
+
link
.
identifier
);
const
vias
=
link
.
arguments
.
vias
.
map
((
s
)
=>
"
via=
"
+
s
).
join
(
"
&
"
);
const
sharer
=
link
.
arguments
.
sharer
?
"
sharer=
"
+
link
.
arguments
.
sharer
:
""
;
const
extras
=
_
.
map
(
link
.
arguments
.
extras
,
(
vals
,
key
)
=>
vals
.
map
((
v
)
=>
key
+
"
=
"
+
v
).
join
(
"
&
"
)
).
join
(
"
&
"
);
const
args
=
[
vias
,
sharer
,
extras
].
filter
(
Boolean
).
join
(
"
&
"
);
if
(
args
)
{
return
uri
+
"
?
"
+
args
;
}
return
uri
;
}
}
This diff is collapsed.
Click to expand it.
src/parser/types.ts
0 → 100644
+
55
−
0
View file @
6325ea83
import
_
from
"
lodash
"
;
export
interface
Arguments
{
vias
:
string
[];
// Either one of the enums or a custom link
sharer
:
string
;
extras
:
{
[
key
:
string
]:
string
[]
};
}
export
interface
LinkContent
{
identifier
:
string
;
arguments
:
Arguments
;
originalLink
:
string
;
}
export
enum
LinkDiscriminator
{
Alias
=
"
ALIAS
"
,
RoomId
=
"
ROOM_ID
"
,
UserId
=
"
USER_ID
"
,
Permalink
=
"
PERMALINK
"
,
GroupId
=
"
GROUP_ID
"
,
ParseFailed
=
"
PARSE_FAILED
"
,
}
export
interface
Alias
extends
LinkContent
{
kind
:
LinkDiscriminator
.
Alias
;
}
export
interface
RoomId
extends
LinkContent
{
kind
:
LinkDiscriminator
.
RoomId
;
}
export
interface
UserId
extends
LinkContent
{
kind
:
LinkDiscriminator
.
UserId
;
}
export
interface
GroupId
extends
LinkContent
{
kind
:
LinkDiscriminator
.
GroupId
;
}
export
interface
Permalink
extends
LinkContent
{
kind
:
LinkDiscriminator
.
Permalink
;
roomKind
:
LinkDiscriminator
.
RoomId
|
LinkDiscriminator
.
Alias
;
roomLink
:
string
;
eventId
:
string
;
}
export
interface
ParseFailed
{
kind
:
LinkDiscriminator
.
ParseFailed
;
originalLink
:
string
;
}
export
type
SafeLink
=
Alias
|
RoomId
|
UserId
|
Permalink
|
GroupId
;
export
type
Link
=
SafeLink
|
ParseFailed
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment