Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
8d2cc1fc
Commit
8d2cc1fc
authored
Mar 13, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Mar 14, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate groups to TypeScript
parent
a19e2e24
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
41 deletions
+27
-41
groups.ts
src/sidebar/helpers/groups.ts
+27
-41
No files found.
src/sidebar/helpers/groups.
j
s
→
src/sidebar/helpers/groups.
t
s
View file @
8d2cc1fc
/**
* @typedef {import('../../types/config').SidebarSettings} SidebarSettings
* @typedef {import('../../types/api').Group} Group
* @typedef {import('../../types/api').GroupIdentifier} GroupIdentifier
*/
import
escapeStringRegexp
from
'escape-string-regexp'
;
import
escapeStringRegexp
from
'escape-string-regexp'
;
import
type
{
Group
,
GroupIdentifier
}
from
'../../types/api'
;
import
type
{
SidebarSettings
}
from
'../../types/config'
;
import
{
serviceConfig
}
from
'../config/service-config'
;
import
{
serviceConfig
}
from
'../config/service-config'
;
/**
/**
...
@@ -12,11 +9,8 @@ import { serviceConfig } from '../config/service-config';
...
@@ -12,11 +9,8 @@ import { serviceConfig } from '../config/service-config';
* are a member? Users may leave private groups unless
* are a member? Users may leave private groups unless
* explicitly disallowed in the service configuration of the
* explicitly disallowed in the service configuration of the
* `settings` object.
* `settings` object.
*
* @param {SidebarSettings} settings
* @return {boolean}
*/
*/
function
allowLeavingGroups
(
settings
)
{
function
allowLeavingGroups
(
settings
:
SidebarSettings
):
boolean
{
const
config
=
serviceConfig
(
settings
);
const
config
=
serviceConfig
(
settings
);
if
(
!
config
)
{
if
(
!
config
)
{
return
true
;
return
true
;
...
@@ -30,12 +24,16 @@ function allowLeavingGroups(settings) {
...
@@ -30,12 +24,16 @@ function allowLeavingGroups(settings) {
* Add an isScopedToUri property to each group indicating whether the uri matches the group's
* Add an isScopedToUri property to each group indicating whether the uri matches the group's
* uri patterns. If no uri patterns are specified, defaults to True.
* uri patterns. If no uri patterns are specified, defaults to True.
*
*
* @param {Group[]} userGroups - groups the user is a member of
* @param userGroups - groups the user is a member of
* @param {Group[]} featuredGroups - all other groups, may include some duplicates from the userGroups
* @param featuredGroups - all other groups, may include some duplicates from the userGroups
* @param {string|null} uri - uri of the current page
* @param uri - uri of the current page
* @param {SidebarSettings} settings
*/
*/
export
function
combineGroups
(
userGroups
,
featuredGroups
,
uri
,
settings
)
{
export
function
combineGroups
(
userGroups
:
Group
[],
featuredGroups
:
Group
[],
uri
:
string
|
null
,
settings
:
SidebarSettings
)
{
const
worldGroup
=
featuredGroups
.
find
(
g
=>
g
.
id
===
'__world__'
);
const
worldGroup
=
featuredGroups
.
find
(
g
=>
g
.
id
===
'__world__'
);
if
(
worldGroup
)
{
if
(
worldGroup
)
{
userGroups
.
unshift
(
worldGroup
);
userGroups
.
unshift
(
worldGroup
);
...
@@ -66,11 +64,7 @@ export function combineGroups(userGroups, featuredGroups, uri, settings) {
...
@@ -66,11 +64,7 @@ export function combineGroups(userGroups, featuredGroups, uri, settings) {
return
groups
;
return
groups
;
}
}
/**
function
isScopedToUri
(
group
:
Group
,
uri
:
string
|
null
):
boolean
{
* @param {Group} group
* @param {string|null} uri
*/
function
isScopedToUri
(
group
,
uri
)
{
/* If a scope check cannot be performed, meaning:
/* If a scope check cannot be performed, meaning:
* - the group doesn't have a scopes attribute
* - the group doesn't have a scopes attribute
* - the group has no scopes.uri_patterns present
* - the group has no scopes.uri_patterns present
...
@@ -83,30 +77,23 @@ function isScopedToUri(group, uri) {
...
@@ -83,30 +77,23 @@ function isScopedToUri(group, uri) {
return
true
;
return
true
;
}
}
/**
function
uriMatchesScopes
(
uri
:
string
,
scopes
:
string
[]):
boolean
{
* @param {string} uri
return
scopes
.
some
(
uriRegex
=>
* @param {string[]} scopes
*/
function
uriMatchesScopes
(
uri
,
scopes
)
{
return
(
scopes
.
find
(
uriRegex
=>
uri
.
match
(
uri
.
match
(
// Convert *'s to .*'s for regex matching and escape all other special characters.
// Convert *'s to .*'s for regex matching and escape all other special characters.
uriRegex
.
split
(
'*'
).
map
(
escapeStringRegexp
).
join
(
'.*'
)
uriRegex
.
split
(
'*'
).
map
(
escapeStringRegexp
).
join
(
'.*'
)
)
)
)
!==
undefined
);
);
}
}
/**
/**
* Find groups in `groups` by GroupIdentifier, which may be either an `id` or
* Find groups in `groups` by GroupIdentifier, which may be either an `id` or
* `groupid`.
* `groupid`.
*
* @param {GroupIdentifier[]} groupIds
* @param {Group[]} groups
* @return {Group[]}
*/
*/
function
findGroupsByAnyIds
(
groupIds
,
groups
)
{
function
findGroupsByAnyIds
(
groupIds
:
GroupIdentifier
[],
groups
:
Group
[]
):
Group
[]
{
return
groups
.
filter
(
return
groups
.
filter
(
g
=>
groupIds
.
includes
(
g
.
id
)
||
(
g
.
groupid
&&
groupIds
.
includes
(
g
.
groupid
))
g
=>
groupIds
.
includes
(
g
.
id
)
||
(
g
.
groupid
&&
groupIds
.
includes
(
g
.
groupid
))
);
);
...
@@ -117,11 +104,10 @@ function findGroupsByAnyIds(groupIds, groups) {
...
@@ -117,11 +104,10 @@ function findGroupsByAnyIds(groupIds, groups) {
* (pubid) or a `groupid` into a list of `id`s by locating associated groups
* (pubid) or a `groupid` into a list of `id`s by locating associated groups
* in the set of all `groups`. Only return entries for groups that can be
* in the set of all `groups`. Only return entries for groups that can be
* found in `groups`.
* found in `groups`.
*
* @param {GroupIdentifier[]} groupIds
* @param {Group[]} groups
* @return {Group["id"][]}
*/
*/
export
function
normalizeGroupIds
(
groupIds
,
groups
)
{
export
function
normalizeGroupIds
(
groupIds
:
GroupIdentifier
[],
groups
:
Group
[]
):
Group
[
'id'
][]
{
return
findGroupsByAnyIds
(
groupIds
,
groups
).
map
(
g
=>
g
.
id
);
return
findGroupsByAnyIds
(
groupIds
,
groups
).
map
(
g
=>
g
.
id
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment