)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Label Thoughts

  1. main:{
  2.   header:{
  3.     {'Tasker'}
  4.     newCol:{'New Column'}
  5.   }
  6.  
  7.   columns:{
  8.     scroll:{
  9.       NoteCols:{}
  10.     }
  11.   }
  12. }
  13.  
  14. NoteCol:{
  15.   title:{['editable', 'focus']
  16.     close:{
  17.       {'×'}
  18.     }
  19.   }
  20.   newNote:{['editable', 'placeholder="New Note..."']}
  21.   hr:{}
  22.   noteContainer:{
  23.     Notes:{}
  24.   }
  25. }
  26.  
  27. Note:{['focus']
  28.   close:{
  29.     {'×'}
  30.   }
  31.   checkbox:{}
  32.   content:{['edtiable']}
  33. }
  34.  
  35. console.log('no errors :D');

The above is actually valid javascript, just a bunch of labels, blocks and a few arrays

// javascript // tricks // ui

Soundex

  1. // Minimum length of Soundex keys.
  2. var minLength = 4
  3.  
  4. // Soundex values belonging to characters.
  5. // This map also includes vowels (with a value of 0) to easily distinguish
  6. // between an unknown value or a vowel.
  7. var map = {}
  8.  
  9. map.a = map.e = map.i = map.o = map.u = map.y = 0
  10. map.b = map.f = map.p = map.v = 1
  11. map.c = map.g = map.j = map.k = map.q = map.s = map.x = map.z = 2
  12. map.d = map.t = 3
  13. map.l = 4
  14. map.m = map.n = 5
  15. map.r = 6
  16.  
  17. /**
  18.  * Get the soundex key from a given value.
  19.  *
  20.  * @param {string} value
  21.  * @param {number} [maxLength=4]
  22.  * @returns {string}
  23.  */
  24. function soundex(value, maxLength) {
  25.   var lowercase = String(value).toLowerCase()
  26.   /** @type {Array.<string|number>} */
  27.   var results = []
  28.   var index = -1
  29.   /** @type {string} */
  30.   var character
  31.   /** @type {number} */
  32.   var previous
  33.   /** @type {number} */
  34.   var phonetics
  35.  
  36.   while (++index < lowercase.length) {
  37.     character = lowercase.charAt(index)
  38.     phonetics = map[character]
  39.  
  40.     if (index === 0) {
  41.       // Initial letter
  42.       results.push(character.toUpperCase())
  43.     } else if (phonetics && phonetics !== previous) {
  44.       // Phonetics value
  45.       results.push(phonetics)
  46.     } else if (phonetics === 0) {
  47.       // Vowel
  48.       phonetics = null
  49.     } else {
  50.       // Unknown character (including H and W)
  51.       phonetics = previous
  52.     }
  53.  
  54.     previous = phonetics
  55.   }
  56.  
  57.   return pad(results.join('')).slice(0, maxLength || minLength)
  58. }
  59.  
  60. /**
  61.  * Pad a given value with zero characters. The function only pads four characters.
  62.  *
  63.  * @param {string} value
  64.  * @returns {string}
  65.  */
  66. function pad(value) {
  67.   var length = minLength - value.length
  68.   var index = -1
  69.  
  70.   while (++index < length) {
  71.     value += '0'
  72.   }
  73.  
  74.   return value
  75. }
  76.  
  77. console.log(
  78.   soundex('Joe'),
  79.   soundex('Smith'),
  80.   soundex('Ed'), 
  81.   soundex('Vilowat')
  82. )

Was talking with someone about Soundex… I grabbed this implementation from the soundex-code npm package from Titus Wormer

Soundex has always been super interesting to me for some reason.

xPath contains (document.evaulate)

  1. // hack to shove some html on the page since snippet.zone
  2. // quick editor does not yet support HTML :P
  3. document.body.innerHTML += `
  4.   <p>This is some text to search...</p>
  5.   <p>Here are some random words "Robot, Cat, Swan, Shadow"...</p>
  6.   <button>Say Hello Robot</button>
  7. `;
  8.  
  9. ///////////
  10.  
  11. // find elements with the word `search`
  12. let els = document.evaluate(
  13.   "//p[contains(., 'search')]",
  14.   document,
  15.   null,
  16.   XPathResult.ANY_TYPE,
  17.   null
  18. );
  19. let el;
  20. while (el = els.iterateNext()) {
  21.   console.log('match "search"', el);
  22. }
  23.  
  24. els = document.evaluate(
  25.   "//body/*[contains(., 'Robot')]",
  26.   document,
  27.   null,
  28.   XPathResult.ANY_TYPE,
  29.   null
  30. );
  31. while (el = els.iterateNext()) {
  32.   console.log('match "Robot"', el);
  33. }

Now that IE is almost dead… we can use xPath – take a look at your console

super Methods Javascript

  1. class Person {
  2.   constructor(name, email) {
  3.     this.name = name;
  4.   }
  5.   toString() {
  6.     return `name: ${this.name}`;
  7.   }
  8. }
  9.  
  10. class Teacher extends Person {
  11.   constructor(name, subject) {
  12.     super(name);
  13.     this.subject = subject;
  14.   }
  15.   toString() {
  16.     return super.toString() + ` subject: ${this.subject}`;
  17.   }
  18. }
  19.  
  20. const teacher = new Teacher('testname', 'testSubject');
  21. console.log(teacher.toString());

This is from an old stackoveflow answer of mine

Thinking About Colors

  1. const { assign } = Object
  2. const { min, max, floor, round, abs } = Math
  3.  
  4. // `hslToRgb` and `rgbToHsl` functions from the amazing Raphael library
  5. // by Dmitry Baranovskiy
  6. // https://github.com/DmitryBaranovskiy/raphael/blob/master/raphael.js
  7.  
  8. export function hslToRgb({ h, s, l, a }) {
  9.   let H = h
  10.   let S = s
  11.   let L = l
  12.  
  13.   s /= 100
  14.   l /= 100
  15.  
  16.   let r, g, b, x, c
  17.   h = (h % 360) / 60
  18.   c = 2 * s * (l < .5 ? l : 1 - l)
  19.   x = c * (1 - abs(h % 2 - 1))
  20.   r = g = b = l - c / 2;
  21.   h = floor(h)
  22.   r += [c, x, 0, 0, x, c][h]
  23.   g += [x, c, c, x, 0, 0][h]
  24.   b += [0, 0, x, c, c, x][h]
  25.   r *= 255; g *= 255; b *= 255
  26.   r = round(r)
  27.   g = round(g)
  28.   b = round(b)
  29.   return { r, g, b, h: H, s: S, l: L, a }
  30. }
  31.  
  32. export function rgbToHsl({ r, g, b, a }) {
  33.   let R = r
  34.   let G = g
  35.   let B = b
  36.  
  37.   r /= 255
  38.   g /= 255
  39.   b /= 255
  40.  
  41.   let h, s, l, w, m, c;
  42.   w = max(r, g, b);
  43.   m = min(r, g, b);
  44.   c = w - m;
  45.   h = (c == 0 ? null :
  46.         w == r ? (g - b) / c :
  47.         w == g ? (b - r) / c + 2 :
  48.                  (r - g) / c + 4)
  49.   h = ((h + 360) % 6) * 60 / 360;
  50.  
  51.   l = (w + m) / 2;
  52.   s = (c == 0 ? 0 : l < .5 ?
  53.     c / (2 * l) :
  54.     c / (2 - 2 * l));
  55.   h *= 360
  56.   s *= 100
  57.   l *= 100
  58.   h = round(h)
  59.   s = round(s)
  60.   l = round(l);
  61.   return { h, s, l, r: R, g: G, b: B, a }
  62. }
  63.  
  64. const fromDec = dec => ({
  65.   r: dec >> 16,
  66.   g: dec >> 8 & 255,
  67.   b: dec & 255
  68. })
  69.  
  70. const fromHex = hex =>
  71.   fromDec(Number('0x' + hex.slice(1)))
  72.  
  73. const components = def =>
  74.   def.match(/[0-9\.]+/g).map(parseFloat)
  75.  
  76. const fromRgb = rgb => {
  77.   const [r, g, b, a] = components(rgb)
  78.   return { r, g, b, a: a ?? 1 }
  79. }
  80.  
  81. const fromHsl = hsl => {
  82.   const [h, s, l, a] = components(hsl)
  83.   return { h, s, l, a: a ?? 1 }
  84. }
  85.  
  86. const utils = {
  87.   hex: (def, o) => assign(o, rgbToHsl(
  88.     fromHex(def)), { a: o.a ?? 1 }
  89.   ),
  90.   rgb: (def, o) => assign(o, rgbToHsl(fromRgb(def))),
  91.   hsl: (def, o) => assign(o, hslToRgb(fromHsl(def))),
  92.   prop: (def, o) => {
  93.     const name = Object.keys(def)[0]
  94.     o[name] = def[name]
  95.     if (name.match(/[rgb]/)) assign(o, rgbToHsl(o))
  96.     if (name.match(/[hsl]/)) assign(o, hslToRgb(o))
  97.  
  98.     return o
  99.   }
  100. }
  101.  
  102. const types = { '#': 'hex', r: 'rgb', h: 'hsl', '[': 'prop' }
  103. const type = str => types[str[0]]
  104.  
  105. const hx = c => {
  106.   const chan = (+c).toString(16)
  107.   if (chan.length === 0) return '00'
  108.   if (chan.length === 1) return '0' + chan
  109.   return chan
  110. };
  111.  
  112. export function shade(def) {
  113.   const o = {
  114.     set(def) {
  115.       const method = utils[type(String(def))]
  116.       if (method != null) method(def, o)
  117.     },
  118.     hsl: _ => `hsla(${o.h}, ${o.s}%, ${o.l}%, ${o.a})`,
  119.     rgb: _ => `rgba(${o.r}, ${o.g}, ${o.b}, ${o.a})`,
  120.     hex: _ => `#${hx(o.r)}${hx(o.g)}${hx(o.b)}`
  121.   }
  122.   o.set(def)
  123.   return o;
  124. }

Wrote this today for something at work… it’s not done yet but figured it was worth a post… will probably post finished version tomorrow…

This can be a little golfed since it is just a demo for work and not code on a real product 😀

snippet.zone ~ 2021-24 /// {s/z}