github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/path/match.go (about)

     1  package path
     2  
     3  import (
     4  	"context"
     5  	"path"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // MATCH reports whether name matches the pattern.
    13  // @param {String} pattern - The pattern.
    14  // @param {String} name - The name.
    15  // @return {Boolean} - True if the name matches the pattern.
    16  func Match(_ context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 2, 2)
    18  
    19  	if err != nil {
    20  		return values.False, err
    21  	}
    22  
    23  	err = core.ValidateType(args[0], types.String)
    24  
    25  	if err != nil {
    26  		return values.False, err
    27  	}
    28  
    29  	err = core.ValidateType(args[1], types.String)
    30  
    31  	if err != nil {
    32  		return values.False, err
    33  	}
    34  
    35  	pattern := args[0].String()
    36  	name := args[1].String()
    37  
    38  	matched, err := path.Match(pattern, name)
    39  
    40  	if err != nil {
    41  		return values.False, core.Error(err, "match")
    42  	}
    43  
    44  	return values.NewBoolean(matched), nil
    45  }