mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 12:17:53 +08:00
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// Package gutil provides utility functions.
|
|
package gutil
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// replaceCharReg is the regular expression object for replacing chars in map keys.
|
|
replaceCharReg, _ = regexp.Compile(`[\-\.\_\s]+`)
|
|
)
|
|
|
|
// MapCopy does memory from map <data> to <copy>.
|
|
func MapCopy(data map[string]interface{}) (copy map[string]interface{}) {
|
|
copy = make(map[string]interface{}, len(data))
|
|
for k, v := range data {
|
|
copy[k] = v
|
|
}
|
|
return
|
|
}
|
|
|
|
// MapPossibleItemByKey tries to find the possible key-value pair for given key with or without
|
|
// cases or chars '-'/'_'/'.'/' '.
|
|
//
|
|
// Note that this function might be of low performance.
|
|
func MapPossibleItemByKey(data map[string]interface{}, key string) (string, interface{}) {
|
|
if v, ok := data[key]; ok {
|
|
return key, v
|
|
}
|
|
replacedKey := replaceCharReg.ReplaceAllString(key, "")
|
|
if v, ok := data[replacedKey]; ok {
|
|
return replacedKey, v
|
|
}
|
|
// Loop for check.
|
|
for k, v := range data {
|
|
if strings.EqualFold(replaceCharReg.ReplaceAllString(k, ""), replacedKey) {
|
|
return k, v
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|