Crates.io | serde_json_lodash |
lib.rs | serde_json_lodash |
version | 0.1.16 |
source | src |
created_at | 2020-12-26 07:48:52.520389 |
updated_at | 2022-01-23 13:34:59.119169 |
description | lodash.js ported version, work with serde_json::Value |
homepage | |
repository | |
max_upload_size | |
id | 327398 |
size | 175,636 |
serde_json::Value with lodash.js spec, makes life easier.
Cargo.toml
[dependencies]
serde_json_lodash = "0.1"
main.rs
#[macro_use] extern crate serde_json_lodash;
use serde_json::json;
fn main() {
// macro style, optional parameters
assert_eq!(
merge!(json!({'a':1}), json!({'b':2}), json!({'c':3})),
json!({'a': 1, 'b': 2, 'c': 3})
);
// fn style, fixed parameters
use serde_json_lodash::merge;
assert_eq!(
merge(json!({'a':1}), json!({'b':2})),
json!({'a': 1, 'b': 2})
);
// `x_`, `_x` helpers for simple types
assert_eq!(capitalize!(json!("FRED")), json!("Fred"));
assert_eq!(x_capitalize!("FRED"), json!("Fred"));
assert_eq!(capitalize_x!(json!("FRED")), "Fred".to_owned());
assert_eq!(x_capitalize_x!("FRED"), "Fred".to_owned());
}
All implements should be same as lodash as possible
How?
fn
and macro
serde_json::Value
, excepts:
_.chunk(array, [size=1])
=> ::chunk(json!([1,2,3]), 2)
, size should be usize
, not Value::Number
std::ops::Fn
as input parameter
_.findIndex(array, predicate, ...)
=> ::find_index(..., predicate: fn(&Value) -> bool, ...)
_.findIndex(...)
=> ::find_index(...) -> isize
, return value should be isize
, not Value::Number
undefined
type in serde_json, so if the original function return undefined
, the ported version should return Value::Null_.get(object, path, [defaultValue])
=> ::get(object, path, defaultValue)
_.merge(object, [...sources])
=> ::merge(object, source)
, but macro could ::merge!(object, source1, source2, ...)
x_
prefix: input is not Value, will be downgrade type
x_capitalize(&str) -> Value
_x
suffix: output is not Value, will be downgrade type
capitalize_x(Value) -> String
x_
and _x
x_capitalize_x(&str) -> &str
, x_add_x(n: Number, n2: Number) -> Number
_.toString([1,2])
, _.toString(123)
=> ::x_to_string(v: &str) -> Value
Examples:
section should be exactly same as the examples in lodash doc.More examples
section, we relied on powerful rust's doc test# Up
./dev.sh
# Watch and test single file
./dev.sh --doc set
# Lint
./lint.sh
# Preview doc
cargo doc --open
# Bump patch version and push
./bump_push.sh
Check lodash.js api
$ npm i
$ node
Welcome to Node.js v15.14.0.
Type ".help" for more information.
> const l = require('lodash')
undefined
> l.toString()
''
>