Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to new string operators #100

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export class Argument {
this.context = context
this.value = argument
if (
this.argumentInfo.type === MirArgumentType.Boolean ||
this.argumentInfo.type === MirArgumentType.Float ||
this.argumentInfo.type === MirArgumentType.Integer ||
this.argumentInfo.type === MirArgumentType.String
this.argumentInfo?.type === MirArgumentType.Boolean ||
this.argumentInfo?.type === MirArgumentType.Float ||
this.argumentInfo?.type === MirArgumentType.Integer ||
this.argumentInfo?.type === MirArgumentType.String
) {
this.argument = null
} else if (this.argumentInfo.type === MirArgumentType.FilterFunction) {
} else if (this.argumentInfo?.type === MirArgumentType.FilterFunction) {
// Check if it's custom filter to know if contains a subscript or a filter function
if (Array.isArray(argument) && Array.isArray(argument[1])) {
this.argument = new Argument(
Expand All @@ -60,13 +60,13 @@ export class Argument {
(argument as [Filter, boolean | string | number])[1]
)
}
} else if (this.argumentInfo.type === MirArgumentType.ReducerFunction) {
} else if (this.argumentInfo?.type === MirArgumentType.ReducerFunction) {
this.argument = new Argument(
this.context,
{ name: 'by', optional: false, type: MirArgumentType.String },
argument as Reducer
)
} else if (this.argumentInfo.type === MirArgumentType.Subscript) {
} else if (this.argumentInfo?.type === MirArgumentType.Subscript) {
this.argument = new Script(
this.context,
argument as MirScript,
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const DEFAULT_SCRIPT_FIRST_TYPE = OutputType.String
export const KIND_OPTIONS = [Kind.HttpGet, Kind.RNG, Kind.HttpPost]
export const CONTENT_TYPE_OPTIONS = {
[Kind.HttpGet]: 'JSON API',
[Kind.HttpHead]: 'JSON API',
[Kind.HttpPost]: 'JSON API',
[Kind.RNG]: 'Binary file',
}
Expand Down
77 changes: 69 additions & 8 deletions src/structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const typeSystem: TypeSystem = {
[Type.Array]: {
[ArrayOperatorName.Count]: [OperatorCode.ArrayCount, OutputType.Integer],
[ArrayOperatorName.Filter]: [OperatorCode.ArrayFilter, OutputType.Same],
//[ArrayOperatorName.Flatten]: [OperatorCode.ArrayFlatten, OutputType.Array],
[ArrayOperatorName.Join]: [OperatorCode.ArrayJoin, OutputType.JoinOutput],
[ArrayOperatorName.GetArray]: [OperatorCode.ArrayGetArray, OutputType.Array],
[ArrayOperatorName.GetBoolean]: [OperatorCode.ArrayGetBoolean, OutputType.Boolean],
[ArrayOperatorName.GetBytes]: [OperatorCode.ArrayGetBytes, OutputType.Bytes],
Expand Down Expand Up @@ -102,6 +102,9 @@ export const typeSystem: TypeSystem = {
[StringOperatorName.ParseXmlMap]: [OperatorCode.StringParseXmlMap, OutputType.Map],
[StringOperatorName.ToLowerCase]: [OperatorCode.StringToLowerCase, OutputType.String],
[StringOperatorName.ToUpperCase]: [OperatorCode.StringToUpperCase, OutputType.String],
[StringOperatorName.Replace]: [OperatorCode.StringReplace, OutputType.String],
[StringOperatorName.Slice]: [OperatorCode.StringSlice, OutputType.String],
[StringOperatorName.Split]: [OperatorCode.StringSplit, OutputType.String],
},
}

Expand Down Expand Up @@ -169,20 +172,20 @@ export const operatorInfos: OperatorInfos = {
(filter: string = 'filter') =>
i18n.t('operator_info_description.array.filter', { filter }),
},
/*[OperatorCode.ArrayFlatten]: {
[OperatorCode.ArrayJoin]: {
type: Type.Array,
name: ArrayOperatorName.Flatten,
name: ArrayOperatorName.Join,
arguments: [
{
name: 'depth',
name: 'separator',
optional: true,
type: MirArgumentType.Integer,
type: MirArgumentType.String,
},
],
outputType: OutputType.Inner,
description: (i18n: I18n) => (depth: string = 'depth') =>
i18n.t('operator_info_description.array.flatten', { depth }),
},*/
description: (i18n: I18n) => (separator: string = '') =>
i18n.t('operator_info_description.array.join', { separator }),
},
[OperatorCode.ArrayGetArray]: {
type: Type.Array,
name: ArrayOperatorName.GetArray,
Expand Down Expand Up @@ -947,6 +950,64 @@ export const operatorInfos: OperatorInfos = {
outputType: OutputType.String,
description: (i18n: I18n) => () => i18n.t('operator_info_description.string.to_upper_case'),
},
[OperatorCode.StringReplace]: {
type: Type.String,
name: StringOperatorName.Replace,
arguments: [
{
name: 'pattern',
optional: false,
type: MirArgumentType.String,
},
{
name: 'replacement',
optional: false,
type: MirArgumentType.String,
},
],
outputType: OutputType.String,
description:
(i18n: I18n) =>
(pattern: string = '', replacement: string = '') =>
i18n.t('operator_info_description.string.replace', { pattern, replacement }),
},
[OperatorCode.StringSlice]: {
type: Type.String,
name: StringOperatorName.Slice,
arguments: [
{
name: 'startIndex',
optional: false,
type: MirArgumentType.Integer,
},
{
name: 'endIndex',
optional: true,
type: MirArgumentType.Integer,
},
],
outputType: OutputType.String,
description:
(i18n: I18n) =>
(startIndex: number = 0, endIndex: number) =>
i18n.t('operator_info_description.string.slice', { startIndex, endIndex }),
},
[OperatorCode.StringSplit]: {
type: Type.String,
name: StringOperatorName.Split,
arguments: [
{
name: 'regex',
optional: true,
type: MirArgumentType.String,
},
],
outputType: OutputType.ArrayString,
description:
(i18n: I18n) =>
(regex: string = "\r") =>
i18n.t('operator_info_description.string.split', { regex }),
},
}

export class Cache {
Expand Down
12 changes: 10 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export enum OutputType {
Same = 'same',
String = 'string',
SubscriptOutput = 'subscriptOutput',
JoinOutput = 'joinOutput',
}

export enum MarkupHierarchicalType {
Expand Down Expand Up @@ -220,13 +221,14 @@ export type KindOptions = Array<Kind>
export enum Kind {
HttpGet = 'HTTP-GET',
HttpPost = 'HTTP-POST',
HttpHead = 'HTTP-HEAD',
RNG = 'RNG',
}

export enum OperatorCode {
ArrayCount = 0x10,
ArrayFilter = 0x11,
//ArrayFlatten = 0x12,
ArrayJoin = 0x12,
ArrayGetArray = 0x13,
ArrayGetBoolean = 0x14,
ArrayGetBytes = 0x15,
Expand Down Expand Up @@ -298,6 +300,9 @@ export enum OperatorCode {
StringParseXmlMap = 0x78,
StringToLowerCase = 0x79,
StringToUpperCase = 0x7a,
StringReplace = 0x7b,
StringSlice = 0x7c,
StringSplit = 0x7d,
}

export enum MirArgumentType {
Expand Down Expand Up @@ -416,7 +421,7 @@ export type OperatorInfos = {
export enum ArrayOperatorName {
Count = 'count',
Filter = 'filter',
//Flatten = 'flatten',
Join = 'join',
GetArray = 'getArray',
GetBoolean = 'getBoolean',
GetBytes = 'getBytes',
Expand Down Expand Up @@ -499,6 +504,9 @@ export enum StringOperatorName {
ParseXmlMap = 'parseXMLMap',
ToLowerCase = 'toLowerCase',
ToUpperCase = 'toUpperCase',
Replace = 'replace',
Slice = 'slice',
Split = 'split',
}

export type OperatorName =
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export function getMarkupInputTypeFromArgumentType(argumentType: MirArgumentType
}

export function getArgumentInfoType(info: ArgumentInfo): MarkupArgumentType {
if (info.type === MirArgumentType.FilterFunction) {
if (info?.type === MirArgumentType.FilterFunction) {
return MarkupArgumentType.SelectFilter
} else if (info.type === MirArgumentType.ReducerFunction) {
} else if (info?.type === MirArgumentType.ReducerFunction) {
return MarkupArgumentType.SelectReduce
} else if (info.type === MirArgumentType.Subscript) {
} else if (info?.type === MirArgumentType.Subscript) {
return MarkupArgumentType.Subscript
} else if (info.type === MirArgumentType.Boolean) {
} else if (info?.type === MirArgumentType.Boolean) {
return MarkupArgumentType.SelectBoolean
} else {
return MarkupArgumentType.Input
Expand Down
Loading