@codeparticle/formal
Version:
A <2kb library for validating data of any kind
1 lines • 1.54 kB
Source Map (JSON)
{"version":3,"sources":["../src/rules/get-prop.ts"],"sourcesContent":["/**\n * @file Check to see if an object has a property, then return the value of that property.\n * @name getProp.js\n * @author Nick Krause\n * @license MIT\n */\n\nimport { createRule } from '../rule'\n\nexport const getProp = (...properties: string[]) => {\n let prop = ``\n let currentObjectPath = {}\n\n return createRule({\n condition: (obj) => {\n currentObjectPath = obj\n\n for (const property of properties) {\n if (currentObjectPath.hasOwnProperty(property)) {\n prop += `.${property}`\n currentObjectPath = currentObjectPath[property]\n } else {\n return false\n }\n }\n\n return true\n },\n message: () => {\n const path = properties.join(`.`)\n\n return `Object does not include property ${path.slice(\n prop.length - 1,\n )} at path .${path}`\n },\n\n transform: (obj) => properties.reduce((acc, key) => acc[key], obj),\n })\n}\n"],"mappings":";;;;;AAAA,AASO,IAAM,UAAU,IAAI,eAAyB;AAClD,MAAI,OAAO;AACX,MAAI,oBAAoB,CAAC;AAEzB,SAAO,WAAW;AAAA,IAChB,WAAW,CAAC,QAAQ;AAClB,0BAAoB;AAEpB,iBAAW,YAAY,YAAY;AACjC,YAAI,kBAAkB,eAAe,QAAQ,GAAG;AAC9C,kBAAQ,IAAI;AACZ,8BAAoB,kBAAkB;AAAA,QACxC,OAAO;AACL,iBAAO;AAAA,QACT;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,IACA,SAAS,MAAM;AACb,YAAM,OAAO,WAAW,KAAK,GAAG;AAEhC,aAAO,oCAAoC,KAAK,MAC9C,KAAK,SAAS,CAChB,cAAc;AAAA,IAChB;AAAA,IAEA,WAAW,CAAC,QAAQ,WAAW,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,GAAG;AAAA,EACnE,CAAC;AACH;","names":[]}