strip

    Recursively strip empty/null/zero values from JSON-like structures

    json
    clean
    strip
    null
    recursive
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    4 months ago
    Downloads
    56

    #0xA672/strip

    A MoonBit library for recursively stripping empty, null, and zero values from JSON-like data structures.

    #Installation

    Add to your moon.mod.json:

    { "deps": { "0xA672/strip": "main" } }

    Then run:

    moon update

    #Quick example

    First, add to your moon.pkg:
    import { "0xA672/strip" @strip }

    Then in your .mbt source:
    ///|
    import {
    "moonbitlang/core/debug"
    }

    fn main {
    let input : @strip.Js = @strip.Ob(Map[String, Js]::of([
    ("name", @strip.St("Alice")),
    ("age", @strip.Nu(0.0)),
    ("address", @strip.Nl),
    ("hobbies", @strip.Ar([])),
    ("meta", @strip.Ob(Map[String, Js]::of([
    ("active", @strip.Bl(false)),
    ("score", @strip.Nu(0.0))
    ])))
    ]))

    let cleaned = @strip.strip(input, true)
    debug_inspect(cleaned)
    // Output: Ob({name: St("Alice"), meta: Ob({active: Bl(false)})})
    }

    💡 Note: The Js enum derives Debug (not Show) because standard library container types (Array, Map) have deprecated Show implementations. Use debug_inspect() or @debug.to_string() for output.

    #API

    #strip(v : Js, oz : Bool) -> Js

    Recursively remove empty/null/zero elements. oz=true strips 0.0, false keeps it. Returns cleaned Js. Empty result defaults to Ob(Map([], capacity=0)).

    #strip_null(v : Js) -> Js

    Only removes Nl (null). Keeps "", 0.0, false, and empty containers.

    #strip_empty(v : Js) -> Js

    Removes Nl, "", and empty arrays/objects. Keeps 0.0 and false.

    #What is considered "empty"?

    TypeStripped when
    Nlalways
    Ar([])always or all children stripped
    Ob({})no fields left after cleaning
    Nu(0.0)oz == true in strip()
    St("")always in strip() / strip_empty()
    Bl(_)never

    #Behavior details

    • Depth limit: Recursion capped at 1000
    • Immutable: Input is never mutated
    • Empty fallback: Fully cleaned tree returns Ob(Map([], capacity=0))

    #Building & testing

    moon build moon test moon fmt

    #License

    Apache-2.0

    pub(all) enum Js {
    Nl
    Bl(Bool)
    Nu(Double)
    St(String)
    Ar(Array[Js])
    Ob(Map[String, Js])
    } derive(
    Debug
    )

    strip

    fn strip(v : Js, oz : Bool) -> Js

    strip_empty

    fn strip_empty(v : Js) -> Js

    strip_null

    fn strip_null(v : Js) -> Js

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io