Skip to content

Instantly share code, notes, and snippets.

@raine
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raine/6d45b6c6b0fc825041e4 to your computer and use it in GitHub Desktop.
Save raine/6d45b6c6b0fc825041e4 to your computer and use it in GitHub Desktop.
1.md
category-list-to-tree = (list) ->
    recurse-category = (c) ->
        label: c?.'name_en' or 'Categories'
        nodes: map recurse-category, filter do
            where-eq do
                level  : c and c.level + 1 or 0
                parent : c and c.id or void
            , list
    recurse-category null

vs.

is-child-of = ->
    where-eq level: it.level + 1, parent: it.id

category-list-to-tree = (list) ->
    recurse-category = (c) ->
        label: c.'name_en'
        nodes: filter (is-child-of c), list |> map recurse-category

    filter (.level is 0), list
    |> map recurse-category
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment