strings

package
v0.0.0-...-5e0ed37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package strings implements basic text processing transforms

Index

Constants

This section is empty.

Variables

View Source
var Contains = &pipescript.Transform{
	Name:          "contains",
	Description:   "Returns true if the given string is found in the datapoint string",
	Documentation: string(resources.MustAsset("docs/transforms/contains.md")),
	Args: []pipescript.TransformArg{
		{
			Description: "The substring to check for",
			Type:        pipescript.ConstArgType,
		},
	},
	Constructor: pipescript.NewBasic(constString, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		d, err := dp.String()
		if err != nil {
			out.Data = false
			return out, nil
		}

		out.Data = strings.Contains(d, consts[0].(string))
		return out, nil
	}),
}
View Source
var Endswith = &pipescript.Transform{
	Name:        "endswith",
	Description: "Returns true if datapoint string ends with the substring given in arg",
	Args: []pipescript.TransformArg{
		{
			Description: "The substring to check for",
			Type:        pipescript.ConstArgType,
			Schema: map[string]interface{}{
				"type": "string",
			},
		},
	},
	InputSchema: map[string]interface{}{
		"type": "string",
	},
	OutputSchema: map[string]interface{}{
		"type": "boolean",
	},
	Constructor: pipescript.NewBasic(constString, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		d, err := dp.String()
		if err != nil {
			out.Data = false
			return out, nil
		}

		out.Data = strings.HasSuffix(d, consts[0].(string))
		return out, nil
	}),
}
View Source
var Regex = &pipescript.Transform{
	Name:          "regex",
	Description:   "Returns true if the given regular expression matches the data string",
	Documentation: string(resources.MustAsset("docs/transforms/regex.md")),
	InputSchema: map[string]interface{}{
		"type": "string",
	},
	OutputSchema: map[string]interface{}{
		"type": "boolean",
	},
	Args: []pipescript.TransformArg{
		{
			Description: "The regular expression to use",
			Type:        pipescript.ConstArgType,
			Schema: map[string]interface{}{
				"type": "string",
			},
		},
	},
	Constructor: pipescript.NewBasic(func(consts []interface{}, pipes []*pipescript.Pipe) ([]interface{}, []*pipescript.Pipe, error) {
		c2 := make([]interface{}, 1)
		c, ok := pipescript.String(consts[0])
		if !ok {
			return nil, nil, errors.New("regex must be a string")
		}
		_, err := regexp.Compile(c)
		if err != nil {
			return nil, nil, err
		}
		c2[0] = c
		return c2, pipes, nil

	}, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		d, err := dp.String()
		if err != nil {
			out.Data = false
			return out, nil
		}
		re, _ := regexp.Compile(consts[0].(string))
		out.Data = re.MatchString(d)
		return out, nil
	}),
}
View Source
var Startswith = &pipescript.Transform{
	Name:        "startswith",
	Description: "Returns true if datapoint string starts with the substring given in arg",
	Args: []pipescript.TransformArg{
		{
			Description: "The substring to check for",
			Type:        pipescript.ConstArgType,
			Schema: map[string]interface{}{
				"type": "string",
			},
		},
	},
	InputSchema: map[string]interface{}{
		"type": "string",
	},
	OutputSchema: map[string]interface{}{
		"type": "boolean",
	},
	Constructor: pipescript.NewBasic(constString, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		d, err := dp.String()
		if err != nil {
			out.Data = false
			return out, nil
		}

		out.Data = strings.HasPrefix(d, consts[0].(string))
		return out, nil
	}),
}
View Source
var Urldomain = &pipescript.Transform{
	Name:        "urldomain",
	Description: "Returns the domain name/host that is used in the given url",
	InputSchema: map[string]interface{}{
		"type": "string",
	},
	OutputSchema: map[string]interface{}{
		"type": "string",
	},
	Constructor: pipescript.NewBasic(nil, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		ds, err := dp.String()
		if err != nil {
			out.Data = ""
			return out, nil
		}
		u, err := url.Parse(ds)
		if err != nil {
			out.Data = ""
			return out, nil
		}
		out.Data = strings.TrimPrefix(u.Host, "www.")
		return out, nil
	}),
}
View Source
var Wc = &pipescript.Transform{
	Name:          "wc",
	Description:   "Returns the number of words in the given string",
	Documentation: string(resources.MustAsset("docs/transforms/wc.md")),
	InputSchema: map[string]interface{}{
		"type": "string",
	},
	OutputSchema: map[string]interface{}{
		"type": "number",
	},
	Constructor: pipescript.NewBasic(nil, func(dp *pipescript.Datapoint, args []*pipescript.Datapoint, consts []interface{}, pipes []*pipescript.Pipe, out *pipescript.Datapoint) (*pipescript.Datapoint, error) {
		ds, err := dp.String()
		if err != nil {
			return nil, err
		}
		out.Data = len(strings.Fields(ds))

		return out, nil
	}),
}

Functions

func Register

func Register()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL