{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/shield/utils.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { LogicRule, Rule } from \"./rules\";\nimport type {\n  LogicRuleInterface,\n  RuleFieldMapInterface,\n  RuleInterface,\n  ShieldRule\n} from \"./types\";\n\n/**\n * Makes sure that a certain field is a rule.\n */\nexport function isRule<TContext extends Record<string, any>>(\n  x: any\n): x is RuleInterface<TContext> {\n  return (\n    x instanceof Rule || (x && x.constructor && x.constructor.name === \"Rule\")\n  );\n}\n\n/**\n * Makes sure that a certain field is a logic rule.\n */\nexport function isLogicRule<TContext extends Record<string, any>>(\n  x: any\n): x is LogicRuleInterface<TContext> {\n  return (\n    x instanceof LogicRule ||\n    (x &&\n      x.constructor &&\n      (x.constructor.name === \"RuleOr\" ||\n        x.constructor.name === \"RuleAnd\" ||\n        x.constructor.name === \"RuleChain\" ||\n        x.constructor.name === \"RuleRace\" ||\n        x.constructor.name === \"RuleNot\" ||\n        x.constructor.name === \"RuleTrue\" ||\n        x.constructor.name === \"RuleFalse\"))\n  );\n}\n\n/**\n * Makes sure that a certain field is a rule or a logic rule.\n */\nexport function isRuleFunction<TContext extends Record<string, any>>(\n  x: any\n): x is ShieldRule<TContext> {\n  return isRule(x) || isLogicRule(x);\n}\n\n/**\n * Determines whether a certain field is rule field map or not.\n */\nexport function isRuleFieldMap<TContext extends Record<string, any>>(\n  x: any\n): x is RuleFieldMapInterface<TContext> {\n  return (\n    typeof x === \"object\" &&\n    Object.values(x).every(rule => isRuleFunction(rule))\n  );\n}\n\n/**\n * Flattens object of particular type by checking if the leaf\n * evaluates to true from particular function.\n */\nexport function flattenObjectOf<T>(\n  obj: { [key: string]: any },\n  f: (x: any) => boolean\n): T[] {\n  const values = Object.keys(obj).reduce<T[]>((acc, key) => {\n    const val = obj[key];\n    if (f(val)) {\n      return [...acc, val];\n    } else if (typeof val === \"object\" && !f(val)) {\n      return [...acc, ...flattenObjectOf(val, f)];\n    } else {\n      return acc;\n    }\n  }, []);\n\n  return values;\n}\n\n/**\n * Returns fallback is provided value is undefined\n */\nexport function withDefault<T>(fallback: T): (value: T | undefined) => T {\n  return value => {\n    if (value === undefined) return fallback;\n    return value;\n  };\n}\n"],"mappings":";;;;;;AA6BA,SAAgB,OACd,GAC8B;CAC9B,OACE,aAAa,QAAS,KAAK,EAAE,eAAe,EAAE,YAAY,SAAS;AAEvE;;;;AAKA,SAAgB,YACd,GACmC;CACnC,OACE,aAAa,aACZ,KACC,EAAE,gBACD,EAAE,YAAY,SAAS,YACtB,EAAE,YAAY,SAAS,aACvB,EAAE,YAAY,SAAS,eACvB,EAAE,YAAY,SAAS,cACvB,EAAE,YAAY,SAAS,aACvB,EAAE,YAAY,SAAS,cACvB,EAAE,YAAY,SAAS;AAE/B;;;;AAKA,SAAgB,eACd,GAC2B;CAC3B,OAAO,OAAO,CAAC,KAAK,YAAY,CAAC;AACnC;;;;AAKA,SAAgB,eACd,GACsC;CACtC,OACE,OAAO,MAAM,YACb,OAAO,OAAO,CAAC,CAAC,CAAC,OAAM,SAAQ,eAAe,IAAI,CAAC;AAEvD;;;;;AAMA,SAAgB,gBACd,KACA,GACK;CAYL,OAXe,OAAO,KAAK,GAAG,CAAC,CAAC,QAAa,KAAK,QAAQ;EACxD,MAAM,MAAM,IAAI;EAChB,IAAI,EAAE,GAAG,GACP,OAAO,CAAC,GAAG,KAAK,GAAG;OACd,IAAI,OAAO,QAAQ,YAAY,CAAC,EAAE,GAAG,GAC1C,OAAO,CAAC,GAAG,KAAK,GAAG,gBAAgB,KAAK,CAAC,CAAC;OAE1C,OAAO;CAEX,GAAG,CAAC,CAEQ;AACd;;;;AAKA,SAAgB,YAAe,UAA0C;CACvE,QAAO,UAAS;EACd,IAAI,UAAU,QAAW,OAAO;EAChC,OAAO;CACT;AACF"}