Simplify SQL string::slug function implementation
This commit is contained in:
parent
508538e0cc
commit
8ca211f96d
5 changed files with 30 additions and 15 deletions
15
Cargo.lock
generated
15
Cargo.lock
generated
|
@ -614,9 +614,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "deunicode"
|
||||
version = "0.4.3"
|
||||
version = "1.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
|
||||
checksum = "f2c9736e15e7df1638a7f6eee92a6511615c738246a052af5ba86f039b65aede"
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
|
@ -2575,15 +2575,6 @@ version = "0.4.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32"
|
||||
|
||||
[[package]]
|
||||
name = "slug"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373"
|
||||
dependencies = [
|
||||
"deunicode",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.8.0"
|
||||
|
@ -2694,6 +2685,7 @@ dependencies = [
|
|||
"bigdecimal",
|
||||
"boa_engine",
|
||||
"chrono",
|
||||
"deunicode",
|
||||
"dmp",
|
||||
"echodb",
|
||||
"futures 0.3.21",
|
||||
|
@ -2715,7 +2707,6 @@ dependencies = [
|
|||
"serde",
|
||||
"sha-1 0.10.0",
|
||||
"sha2",
|
||||
"slug",
|
||||
"storekey",
|
||||
"surrealdb-derive",
|
||||
"thiserror",
|
||||
|
|
|
@ -22,6 +22,7 @@ boa = { version = "0.15.0", package = "boa_engine", optional = true }
|
|||
channel = { version = "1.6.1", package = "async-channel" }
|
||||
chrono = { version = "0.4.19", features = ["serde"] }
|
||||
derive = { version = "0.3.0", package = "surrealdb-derive" }
|
||||
deunicode = "1.3.1"
|
||||
dmp = "0.1.1"
|
||||
echodb = { version = "0.3.0", optional = true }
|
||||
executor = { version = "1.4.1", package = "async-executor", optional = true }
|
||||
|
@ -44,7 +45,6 @@ scrypt = "0.10.0"
|
|||
serde = { version = "1.0.138", features = ["derive"] }
|
||||
sha-1 = "0.10.0"
|
||||
sha2 = "0.10.2"
|
||||
slug = "0.1.4"
|
||||
storekey = "0.3.0"
|
||||
thiserror = "1.0.31"
|
||||
tikv = { version = "0.1.0", package = "tikv-client", optional = true }
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::ctx::Context;
|
||||
use crate::err::Error;
|
||||
use crate::fnc::util::string;
|
||||
use crate::sql::value::Value;
|
||||
use slug::slugify;
|
||||
|
||||
pub fn concat(_: &Context, args: Vec<Value>) -> Result<Value, Error> {
|
||||
Ok(args.into_iter().map(|x| x.as_string()).collect::<Vec<_>>().concat().into())
|
||||
|
@ -56,7 +56,7 @@ pub fn slice(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
|||
}
|
||||
|
||||
pub fn slug(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
||||
Ok(slugify(&args.remove(0).as_string()).into())
|
||||
Ok(string::slug(&args.remove(0).as_string()).into())
|
||||
}
|
||||
|
||||
pub fn split(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod geo;
|
||||
pub mod http;
|
||||
pub mod math;
|
||||
pub mod string;
|
||||
|
|
23
lib/src/fnc/util/string/mod.rs
Normal file
23
lib/src/fnc/util/string/mod.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use deunicode::deunicode;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
|
||||
static SIMPLES: Lazy<Regex> = Lazy::new(|| Regex::new("[^a-z0-9-_]").unwrap());
|
||||
static HYPHENS: Lazy<Regex> = Lazy::new(|| Regex::new("-+").unwrap());
|
||||
|
||||
pub fn slug<S: AsRef<str>>(s: S) -> String {
|
||||
// Get a reference
|
||||
let s = s.as_ref();
|
||||
// Convert unicode to ascii
|
||||
let s = deunicode(s);
|
||||
// Convert strgin to lowercase
|
||||
let s = s.to_ascii_lowercase();
|
||||
// Replace any non-simple characters
|
||||
let s = SIMPLES.replace_all(s.as_ref(), "-");
|
||||
// Replace any duplicated hyphens
|
||||
let s = HYPHENS.replace_all(s.as_ref(), "-");
|
||||
// Remove any surrounding hyphens
|
||||
let s = s.trim_matches('-');
|
||||
// Return the string
|
||||
s.to_owned()
|
||||
}
|
Loading…
Reference in a new issue