github.com/influx6/npkg@v0.8.8/npattrn/utils.go (about)

     1  package npattrn
     2  
     3  import (
     4  	"path"
     5  	"path/filepath"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  //==============================================================================
    11  
    12  var endless = regexp.MustCompile(`/\*$`)
    13  
    14  //IsEndless returns true/false if the npattrn as a /*
    15  func IsEndless(s string) bool {
    16  	return endless.MatchString(s)
    17  }
    18  
    19  //==============================================================================
    20  
    21  // moreSlashes this to check for more than one forward slahes
    22  var moreSlashes = regexp.MustCompile(`\/+`)
    23  
    24  // CleanSlashes cleans all double forward slashes into one
    25  func CleanSlashes(p string) string {
    26  	if strings.Contains(p, "\\") {
    27  		return moreSlashes.ReplaceAllString(filepath.ToSlash(p), "/")
    28  	}
    29  	return moreSlashes.ReplaceAllString(p, "/")
    30  }
    31  
    32  //==============================================================================
    33  
    34  //RemoveCurly removes '{' and '}' from any string
    35  func RemoveCurly(s string) string {
    36  	return strings.TrimPrefix(strings.TrimSuffix(s, "}"), "{")
    37  }
    38  
    39  //==============================================================================
    40  
    41  //RemoveBracket removes '[' and ']' from any string
    42  func RemoveBracket(s string) string {
    43  	return strings.TrimPrefix(strings.TrimSuffix(s, "]"), "[")
    44  }
    45  
    46  //==============================================================================
    47  
    48  //SplitPattern splits a npattrn with the '/'
    49  func SplitPattern(c string) []string {
    50  	return strings.Split(c, "/")
    51  }
    52  
    53  // addSlash appends a / infront of the giving string if not there.
    54  func addSlash(ps string) string {
    55  	if strings.HasPrefix(ps, "/") {
    56  		return ps
    57  	}
    58  
    59  	return "/" + ps
    60  }
    61  
    62  //==============================================================================
    63  
    64  //TrimEndSlashe removes the '/' at the end of string.
    65  func TrimEndSlashe(c string) string {
    66  	return strings.TrimSuffix(cleanPath(c), "/")
    67  }
    68  
    69  //==============================================================================
    70  
    71  //TrimSlashes removes the '/' at the beginning and end of string.
    72  func TrimSlashes(c string) string {
    73  	return strings.TrimSuffix(strings.TrimPrefix(cleanPath(c), "/"), "/")
    74  }
    75  
    76  //==============================================================================
    77  
    78  //SplitPatternAndRemovePrefix splits a npattrn with the '/'
    79  func SplitPatternAndRemovePrefix(c string) []string {
    80  	return strings.Split(strings.TrimPrefix(cleanPath(c), "/"), "/")
    81  }
    82  
    83  //==============================================================================
    84  
    85  var morespecial = regexp.MustCompile(`{\w+:[\w\W]+}`)
    86  
    87  // HasKeyParam returns true/false if the special npattrn {:[..]} exists in the string
    88  func HasKeyParam(p string) bool {
    89  	return morespecial.MatchString(p)
    90  }
    91  
    92  // CheckPriority is used to return the priority of a npattrn.
    93  // 0 for highest(when no parameters).
    94  // 1 for restricted parameters({id:[]}).
    95  // 2 for unrestricted parameter.
    96  //
    97  // The first parameter seen is used for rating.
    98  // The ratings go from highest to lowest .i.e (0-2).
    99  func CheckPriority(patt string) int {
   100  	sets := splitPattern(patt)
   101  
   102  	for _, so := range sets {
   103  		if morespecial.MatchString(so) {
   104  			return 1
   105  		}
   106  
   107  		if special.MatchString(so) {
   108  			return 2
   109  		}
   110  
   111  		continue
   112  	}
   113  
   114  	return 0
   115  }
   116  
   117  //cleanPattern cleans any /* * npattrn found
   118  func cleanPattern(patt string) string {
   119  	cleaned := endless.ReplaceAllString(patt, "")
   120  	return morespecial.ReplaceAllString(cleaned, "/")
   121  }
   122  
   123  //==============================================================================
   124  
   125  // CleanPath provides a public path cleaner
   126  func CleanPath(p string) string {
   127  	return cleanPath(p)
   128  }
   129  
   130  // cleanPath returns the canonical path for p, eliminating . and .. elements.
   131  // Borrowed from the net/http package.
   132  func cleanPath(p string) string {
   133  	if p == "" {
   134  		return "/"
   135  	}
   136  	if p[0] != '/' {
   137  		p = "/" + p
   138  	}
   139  	np := path.Clean(p)
   140  	// path.Clean removes trailing slash except for root;
   141  	// put the trailing slash back if necessary.
   142  	if p[len(p)-1] == '/' && np != "/" {
   143  		np += "/"
   144  	}
   145  	return np
   146  }
   147  
   148  //==============================================================================
   149  
   150  var special = regexp.MustCompile(`{\w+:[\w\W]+}|:[\w]+`)
   151  
   152  // HasParam returns true/false if the special npattrn {:[..]} exists in the string
   153  func HasParam(p string) bool {
   154  	return special.MatchString(p)
   155  }
   156  
   157  //==============================================================================
   158  
   159  var picker = regexp.MustCompile(`^:[\w\W]+$`)
   160  
   161  // HasPick matches string of type :id,:name
   162  func HasPick(p string) bool {
   163  	return picker.MatchString(p)
   164  }
   165  
   166  //==============================================================================
   167  
   168  var specs = regexp.MustCompile(`\n\t\s+`)
   169  var paramd = regexp.MustCompile(`^{[\w\W]+}$`)
   170  var anyvalue = `[\w\W]+`
   171  
   172  //YankSpecial provides a means of extracting parts of form `{id:[\d+]}`
   173  func YankSpecial(val string) (string, string, bool) {
   174  	if HasPick(val) {
   175  		cls := strings.TrimPrefix(val, ":")
   176  		return cls, anyvalue, true
   177  	}
   178  
   179  	if !paramd.MatchString(val) {
   180  		cls := specs.ReplaceAllString(val, "")
   181  		return cls, cls, false
   182  	}
   183  
   184  	part := strings.Split(removeCurly(val), ":")
   185  	return part[0], removeBracket(part[1]), true
   186  }
   187  
   188  //==============================================================================
   189  
   190  func removeCurly(s string) string {
   191  	return strings.TrimPrefix(strings.TrimSuffix(s, "}"), "{")
   192  }
   193  
   194  func removeBracket(s string) string {
   195  	return strings.TrimPrefix(strings.TrimSuffix(s, "]"), "[")
   196  }
   197  
   198  func splitPattern(c string) []string {
   199  	parts := strings.Split(c, "/")
   200  
   201  	// Re-add the first slash to respect root supremacy.
   202  	if len(parts) > 0 && parts[0] == "" {
   203  		parts[0] = "/"
   204  	}
   205  
   206  	return parts
   207  }
   208  
   209  //==============================================================================
   210  
   211  // stripAndClean strips the slahes from the path.
   212  func stripAndClean(c string) string {
   213  	return CleanSlashes(strings.Replace(strings.TrimSuffix(strings.TrimSuffix(c, "/*"), "/"), "#", "/", -1))
   214  }
   215  
   216  // stripAndCleanButHash strips the slahes from the path.
   217  func stripAndCleanButHash(c string) string {
   218  	return CleanSlashes(strings.TrimSuffix(strings.TrimSuffix(c, "/*"), "/"))
   219  }