{"version":3,"sources":["jsdelivr-header.js","/npm/stateguy@0.0.2/stateguy.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA,ACWA,IAAI,MAGJ,MAAM,KAAQ,IACZ,GAAI,MACF,MAAM,IAAI,MAAM,8CAGlB,MAAQ,IAAK,GACb,WAAU,EAGN,YAAc,CAAC,EAAM,KACzB,IAAI,EAAW,CAAC,EAEhB,OAAO,KAAK,GAAS,SAAQ,IACC,iBAAjB,EAAQ,GACjB,YAAY,EAAQ,IAEpB,EAAS,GAAO,EAAQ,EAC1B,IAGF,MAAQ,IAAK,MAAO,CAAC,GAAO,GAC5B,WAAU,EAON,MAAQ,IAAI,KAChB,IAAI,EAAO,MAAM,EAAW,IAM5B,OAJI,EAAW,OAAS,GACtB,EAAW,MAAM,GAAG,SAAQ,GAAY,EAAO,EAAK,KAG/C,GAIH,SAAW,CAAC,EAAM,KACtB,QAAoB,IAAhB,MAAM,GACR,MAAM,IAAI,MAAM,8FAGlB,YAAY,EAAM,EAAO,EAGrB,UAAY,KAiBG,SAAS,iBAAiB,eAClC,SAAQ,GACV,EAAQ,UAlBM,CAAC,IAEtB,MAGM,EAHS,EAAU,MAAM,GAAG,MAAM,KAGpB,GAAG,MAAM,KAI7B,GAAqB,iBAFP,SAAS,GAGrB,MAAM,IAAI,MAAM,mHAGlB,OAAO,SAAS,EAAI,EAKO,CAAe,EAAQ,YACnD,gBAGY,CACb,UACA,YACA","file":"/npm/stateguy@0.0.2/stateguy.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/stateguy@0.0.2/stateguy.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/*\nSTATEGUY\nA friendly guy who lives inside your browser and manages your state.\nby Camden Mecklem, 2021\nreleased under MIT license\n\nStateguy is a simple frameworkless state management system inspired by React-Redux.\nYou can tie DOM elements to state object properties by assigning a class name with the\n\"sg-\" prefix. For example:\n\n<span class=\"sg-city-population\"></span>\n\nThis span element will display the value from ``state.city.population``. Stateguy will\nautomatically update the element each time the state updates.\n*/\n\n// State is stored in a private object that is not directly accessible from outside stateguy.js.\nlet state\n\n// Initialize a state object\nconst init = (stateObject) => {\n  if (state) {\n    throw new Error('Trying to initialize state more than once.')\n  }\n\n  state = { ...stateObject }\n  updateDOM()\n}\n\nconst updateState = (name, payload) => {\n  let newState = {}\n\n  Object.keys(payload).forEach(key => {\n    if (typeof payload[key] === 'object') {\n      updateState(payload[key])\n    } else {\n      newState[key] = payload[key]\n    }\n  })\n\n  state = { ...state, [name]: payload }\n  updateDOM()\n}\n\n// Accepts an arbitrary number of string arguments.\n// Traverses the state by using each arg as a property.\n// e.g. fetch('developer') => { name: 'Camden', age: 24 }\n//       fetch('developer', 'name') => 'Camden'\nconst fetch = (...properties) => {\n  let item = state[properties[0]]\n\n  if (properties.length > 1) {\n    properties.slice(1).forEach(property => item = item[property])\n  }\n\n  return item\n}\n\n// Accepts an object and overwrites anything in state that matches it\nconst dispatch = (name, payload) => {\n  if (state[name] === undefined) {\n    throw new Error('Trying to dispatch to unknown state property. Make sure to add it to your init call first.')\n  }\n\n  updateState(name, payload)\n}\n\nconst updateDOM = () => {\n  const parseClassName = (className) => {\n    // remove 'sg-' from the path\n    const sgPath = className.slice(3).split(' ')\n\n    // ignore unrelated classes\n    const path = sgPath[0].split('-')\n\n    const value = fetch(...path)\n\n    if (typeof value === 'object') {\n      throw new Error('Trying to pass an object to the DOM. Only datatypes that can be cast into a string can be displayed on the DOM.')\n    }\n\n    return fetch(...path)\n  }\n\n  const sgElements = document.querySelectorAll(`[class*=sg-`)\n  sgElements.forEach(element => {\n    return element.innerHTML = parseClassName(element.className)\n  })\n}\n\nexport default {\n  init,\n  fetch,\n  dispatch\n}"]}