-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #129
- Loading branch information
Showing
20 changed files
with
1,433 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Resource } from 'rdf-object'; | ||
|
||
/** | ||
* Transforms a resource based on the contents of an override step. | ||
*/ | ||
export interface IOverrideStep { | ||
/** | ||
* Determines if this handler can apply the given override step to the resource. | ||
* | ||
* @param config - The resource to override. | ||
* @param step - The override step to apply. | ||
* | ||
* @returns true if this handler should be used. | ||
*/ | ||
canHandle: (config: Resource, step: Resource) => boolean; | ||
|
||
/** | ||
* Applies the changes described in the given override step to the resource. | ||
* | ||
* @param config - The resource to override. | ||
* @param step - The override step to apply. | ||
* | ||
* @returns The modified resource. | ||
*/ | ||
handle: (config: Resource, step: Resource) => Resource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Resource } from 'rdf-object'; | ||
import { PREFIX_OO } from '../../rdf/Iris'; | ||
import type { IOverrideStep } from './IOverrideStep'; | ||
import { extractOverrideStepFields, findResourceIndex, getPropertyResourceList } from './OverrideUtil'; | ||
|
||
/** | ||
* Override step that inserts elements in a list after a specific element. | ||
* | ||
* Uses the following override step fields: | ||
* - `overrideParameter`: Parameter of the original object that contains the list. | ||
* - `overrideTarget`: Element already in the list that is used as reference. This can be a named node or a literal. | ||
* - `overrideValue`: Element(s) to insert immediately after the target element. | ||
*/ | ||
export class OverrideListInsertAfter implements IOverrideStep { | ||
public canHandle(config: Resource, step: Resource): boolean { | ||
return step.property.type.value === PREFIX_OO('OverrideListInsertAfter'); | ||
} | ||
|
||
public handle(config: Resource, step: Resource): Resource { | ||
const { parameters, targets, values } = extractOverrideStepFields(step, { parameters: 1, targets: 1 }); | ||
|
||
const list = getPropertyResourceList(config, parameters[0]); | ||
|
||
const index = findResourceIndex(list, targets[0]); | ||
|
||
// +1 so we start after the selected element | ||
list.splice(index + 1, 0, ...values); | ||
|
||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Resource } from 'rdf-object'; | ||
import { PREFIX_OO } from '../../rdf/Iris'; | ||
import { ErrorResourcesContext } from '../../util/ErrorResourcesContext'; | ||
import type { IOverrideStep } from './IOverrideStep'; | ||
import { extractOverrideStepFields, getPropertyResourceList } from './OverrideUtil'; | ||
|
||
/** | ||
* Override step that inserts elements in a list at a specific index. | ||
* A negative index can be used to count from the back of the list. | ||
* An index of `-0` can be used to insert at the end of the list. | ||
* | ||
* Uses the following override step fields: | ||
* - `overrideParameter`: Parameter of the original object that contains the list. | ||
* - `overrideTarget`: A literal containing the index. | ||
* - `overrideValue`: Element(s) to insert at the chosen index. | ||
*/ | ||
export class OverrideListInsertAt implements IOverrideStep { | ||
public canHandle(config: Resource, step: Resource): boolean { | ||
return step.property.type.value === PREFIX_OO('OverrideListInsertAt'); | ||
} | ||
|
||
public handle(config: Resource, step: Resource): Resource { | ||
const { parameters, targets, values } = extractOverrideStepFields(step, { parameters: 1, targets: 1 }); | ||
|
||
const list = getPropertyResourceList(config, parameters[0]); | ||
|
||
const val = targets[0].value; | ||
if (!/^-?\d+$/u.test(val)) { | ||
throw new ErrorResourcesContext(`Invalid index in Override step OverrideListInsertAt for parameter ${parameters[0].value}: ${val}`, { | ||
config, | ||
step, | ||
}); | ||
} | ||
|
||
// Support adding elements at the end using -0 | ||
if (val === '-0') { | ||
list.push(...values); | ||
} else { | ||
const index = Number.parseInt(val, 10); | ||
list.splice(index, 0, ...values); | ||
} | ||
|
||
return config; | ||
} | ||
} |
Oops, something went wrong.