<commit_before>assert = require 'assert'

exports.draw = (world) ->
  SVG = world.SVG()

  draw_self = (world) ->
    #console.log "draw_self #{world}"
    dict = {
      class: world.labels(['draw', 'self'])
      stroke: world.get 'stroke'
      fill: world.get 'fill'
    }
    dict['stroke'] = 'goldenrod' if world.get('selected')
    paths = world.get 'path'
    return undefined unless paths?
    paths = [paths] if !world.is_array(paths)
    elements = paths.map (path) ->
      dict['d'] = path
      SVG.path dict
    return elements unless world.get('name')?
    elements.push SVG.text world.get('name_style'), world.bind() -> world.get('name')
    elements
    
  draw_children = (world) -> world.map_children(draw_world)  

  draw_world = (world) ->
    #console.log "draw_world #{world} kids:#{world.has_children()}", world
    dict = {
      class: world.labels(['draw', 'world'])
      transform: world.bind() -> world.get('transform')
    }
    clicker = world.get_raw 'click'
    dict['click'] = -> clicker(world) if clicker? and !world.has_children()
    SVG.g dict, world.bind() ->
      paths = draw_self(world)
      paths = [] unless paths?
      paths = paths .all() unless _.isArray(paths)
      child_paths = draw_children(world)
      child_paths = child_paths.all() unless _.isArray(child_paths)
      for child in child_paths
        paths.push child
      paths

  device_width = world.get('device_width') or 1024
  console.log "draw", device_width, world
  SVG.svg {
    xmlns: "http://www.w3.org/2000/svg"
    "xmlns:xlink": "http://www.w3.org/1999/xlink"
    class: "svg_grid #{world}"
    width: device_width  
    height: device_width * 3 / 4  
  }, [
    draw_world(world) 
  ]<commit_msg>Refactor to normalize all paths<commit_after>assert = require 'assert'

normalize = (paths) ->
  return [] unless paths
  if _.isArray(paths) then paths else paths.all()
  
exports.draw = (world) ->
  SVG = world.SVG()

  draw_self = (world) ->
    #console.log "draw_self #{world}"
    dict = {
      class: world.labels(['draw', 'self'])
      stroke: world.get 'stroke'
      fill: world.get 'fill'
    }
    dict['stroke'] = 'goldenrod' if world.get('selected')
    paths = world.get 'path'
    return undefined unless paths?
    paths = [paths] if !world.is_array(paths)
    elements = paths.map (path) ->
      dict['d'] = path
      SVG.path dict
    return elements unless world.get('name')?
    elements.push SVG.text world.get('name_style'), world.bind() -> world.get('name')
    elements
    
  draw_children = (world) -> world.map_children(draw_world)  

  draw_world = (world) ->
    #console.log "draw_world #{world} kids:#{world.has_children()}", world
    dict = {
      class: world.labels(['draw', 'world'])
      transform: world.bind() -> world.get('transform')
    }
    clicker = world.get_raw 'click'
    dict['click'] = -> clicker(world) if clicker? and !world.has_children()
    SVG.g dict, world.bind() ->
      paths = normalize draw_self(world)
      child_paths = normalize draw_children(world)
      for child in child_paths
        paths.push child
      paths

  device_width = world.get('device_width') or 1024
  console.log "draw", device_width, world
  SVG.svg {
    xmlns: "http://www.w3.org/2000/svg"
    "xmlns:xlink": "http://www.w3.org/1999/xlink"
    class: "svg_grid #{world}"
    width: device_width  
    height: device_width * 3 / 4  
  }, [
    draw_world(world) 
  ]
{'fraction_non_alphanumeric': 0.15274949083503056, 'fraction_numerical': 0.009310445155659005, 'mean_word_length': 2.902383654937571, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '179840', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1138, 'n_words': 263}