Advertisement
Guest User

foobar.ts

a guest
Aug 1st, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // セレクタビルダー、スペルミス回避のため、の?
  2.  
  3. class Foo {
  4.     state: string;
  5.     constructor(s: string) { this.state = s; }
  6.     bar(s: string) { return new Bar(`${this.state}${s}`); }
  7.     anyTag()    { return this.bar('*'       ); }
  8.     button()    { return this.bar('button'  ); }
  9.     header()    { return this.bar('header'  ); }
  10.     img()       { return this.bar('img'     ); }
  11.     li()        { return this.bar('li'      ); }
  12.     nav()       { return this.bar('nav'     ); }
  13.     noTag()     { return this.bar(''        ); }
  14.     output()    { return this.bar('output'  ); }
  15.     p()         { return this.bar('p'       ); }
  16.     textarea()  { return this.bar('textarea'); }
  17.     ul()        { return this.bar('ul'      ); }
  18. }
  19.  
  20. class Bar {
  21.     state: string;
  22.     constructor(s: string) { this.state = s; }
  23.     foo(s: string) {
  24.         if (s === '') {
  25.             return new Foo(`${this.state} `);
  26.         } else {
  27.             return new Foo(`${this.state} ${s} `);
  28.         }
  29.     }
  30.     withClass(s: string) {
  31.         if (s.startsWith('.') || s.startsWith(':')) {
  32.             this.state += s;
  33.         } else {
  34.             this.state += '.' + s;
  35.         }
  36.         return this;
  37.     }
  38.     withId(s: string) {
  39.         if (s.startsWith('#')) {
  40.             this.state += s;
  41.         } else {
  42.             this.state += '#' + s;
  43.         }
  44.         return this;
  45.     }
  46.     and()        { return this.foo(','); }
  47.     child()      { return this.foo('>'); }
  48.     next()       { return this.foo('+'); }
  49.     descendant() { return this.foo('' ); }
  50.     active() { return this.withClass(':active'); }
  51.     focus()  { return this.withClass(':focus' ); }
  52.     hover()  { return this.withClass(':hover' ); }
  53.     scope()  { return this.withClass(':scope' ); }
  54.     toString() { return this.state; }
  55. }
  56.  
  57. function fb() { return new Foo(''); }
  58.  
  59. /*
  60. 驚くほどに可読性悪い・・・(スペルミス回避のためだけに使うのは微妙ぽ)
  61.  
  62. selector:
  63.     header > nav li button
  64. builder:
  65.     fb().header()
  66.         .child().nav()
  67.         .descendant().li()
  68.         .descendant().button()
  69.         .toString()
  70.  
  71.  
  72. selector:
  73.     header > nav > output
  74. builder:
  75.     fb().header()
  76.         .child().nav()
  77.         .child().output()
  78.         .toString()
  79.  
  80.  
  81. selector:
  82.     .memo > ul
  83. builder:
  84.     const MEMO = 'memo';
  85.     fb().noTag().withClass(MEMO)
  86.         .child().ul()
  87.         .toString()
  88.  
  89.  
  90. selector:
  91.     .timeline > ul
  92. builder:
  93.     const TIMELINE = 'timeline';
  94.     fb().noTag().withClass(TIMELINE)
  95.         .child().ul()
  96.         .toString()
  97.  
  98.  
  99. selector:
  100.     #hoge > ul
  101. builder:
  102.     const HOGE = 'hoge';
  103.     fb().noTag().withId(HOGE)
  104.         .child().ul()
  105.         .toString()
  106.  
  107. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement