<commit_before>package doom;

import js.html.Event;
import js.html.Element;

abstract AttributeValue(AttributeValueImpl) from AttributeValueImpl to AttributeValueImpl {
  @:from static public function fromString(s : String) : AttributeValue
    return StringAttribute(s);

  @:from static public function fromMap(map : Map<String, Bool>) : AttributeValue {
    var values = [];
    for(key in map.keys())
      if(map.get(key))
        values.push(key);
    return StringAttribute(values.join(" "));
  }

  @:from static public function fromBool(b : Bool) : AttributeValue
    return BoolAttribute(b);

  @:from static public function fromHandler(f : Void -> Void) : AttributeValue
    return EventAttribute(function(e : Event) {
      e.preventDefault();
      f();
    });

  @:from static public function fromEventHandler<T : Event>(f : T -> Void) : AttributeValue
    return EventAttribute(f);

  @:from static public function fromElementHandler<T : Element>(f : T -> Void) : AttributeValue
    return EventAttribute(function(e : Event) {
      e.preventDefault();
      var input : T = cast e.target;
      f(input);
    });

  @:op(A==B)
  public function equalsTo(that : AttributeValue)
    return switch [this, that] {
      case [BoolAttribute(a), BoolAttribute(b)]: a == b;
      case [StringAttribute(a), StringAttribute(b)]: a == b;
      case [_, _]: false;
    };

  @:op(A!=B)
  public function notEqualsTo(that : AttributeValue)
    return !equalsTo(that);
}

enum AttributeValueImpl {
  BoolAttribute(b : Bool);
  StringAttribute(s : String);
  EventAttribute<T : Event>(f : T -> Void);
}<commit_msg>added more attribute value helpers<commit_after>package doom;

import js.html.Event;
import js.html.Element;
import thx.Ints;
import thx.Floats;

abstract AttributeValue(AttributeValueImpl) from AttributeValueImpl to AttributeValueImpl {
  @:from static public function fromString(s : String) : AttributeValue
    return StringAttribute(s);

  @:from static public function fromMap(map : Map<String, Bool>) : AttributeValue {
    var values = [];
    for(key in map.keys())
      if(map.get(key))
        values.push(key);
    return StringAttribute(values.join(" "));
  }

  @:from static public function fromBool(b : Bool) : AttributeValue
    return BoolAttribute(b);

  @:from static public function fromHandler(f : Void -> Void) : AttributeValue
    return EventAttribute(function(e : Event) {
      e.preventDefault();
      f();
    });

  @:from static public function fromEventHandler<T : Event>(f : T -> Void) : AttributeValue
    return EventAttribute(f);

  @:from static public function fromElementHandler<T : Element>(f : T -> Void) : AttributeValue
    return EventAttribute(function(e : Event) {
      e.preventDefault();
      var input : T = cast e.target;
      f(input);
    });

  @:from static public function fromStringValueHandler(f : String -> Void) : AttributeValue
    return EventAttribute(function(e : Event) {
      e.preventDefault();
      var value : String = dots.Dom.getValue(cast e.target);
      f(value);
    });

  @:from static public function fromBoolValueHandler(f : Bool -> Void) : AttributeValue
    return EventAttribute(function(e : Event) {
      e.preventDefault();
      var value : Bool = (cast e.target : js.html.InputElement).checked;
      f(value);
    });

  @:from static public function fromIntValueHandler(f : Int -> Void) : AttributeValue
    return fromStringValueHandler(function(s : String) {
      if(Ints.canParse(s))
        f(Ints.parse(s));
    });

  @:from static public function fromFloatValueHandler(f : Float -> Void) : AttributeValue
    return fromStringValueHandler(function(s : String) {
      if(Floats.canParse(s))
        f(Floats.parse(s));
    });

  @:op(A==B)
  public function equalsTo(that : AttributeValue)
    return switch [this, that] {
      case [BoolAttribute(a), BoolAttribute(b)]: a == b;
      case [StringAttribute(a), StringAttribute(b)]: a == b;
      case [_, _]: false;
    };

  @:op(A!=B)
  public function notEqualsTo(that : AttributeValue)
    return !equalsTo(that);
}

enum AttributeValueImpl {
  BoolAttribute(b : Bool);
  StringAttribute(s : String);
  EventAttribute<T : Event>(f : T -> Void);
}
{'fraction_non_alphanumeric': 0.12840559109215827, 'fraction_numerical': 0.0, 'mean_word_length': 3.6395604395604395, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3920193', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1241, 'n_words': 327}