@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
1 lines • 3.61 kB
Source Map (JSON)
{"version":3,"file":"FlagUnorderedStrategy.cjs","names":["Option","PrefixedStrategy"],"sources":["../../../../../src/lib/utils/strategies/FlagUnorderedStrategy.ts"],"sourcesContent":["import { PrefixedStrategy } from '@sapphire/lexure';\nimport { Option } from '@sapphire/result';\n\n/**\n * The strategy options used in Sapphire.\n */\nexport interface FlagStrategyOptions {\n\t/**\n\t * The accepted flags. Flags are key-only identifiers that can be placed anywhere in the command. Two different types are accepted:\n\t * * An array of strings, e.g. [`silent`].\n\t * * A boolean defining whether the strategy should accept all keys (`true`) or none at all (`false`).\n\t * @default []\n\t */\n\tflags?: readonly string[] | boolean;\n\n\t/**\n\t * The accepted options. Options are key-value identifiers that can be placed anywhere in the command. Two different types are accepted:\n\t * * An array of strings, e.g. [`silent`].\n\t * * A boolean defining whether the strategy should accept all keys (`true`) or none at all (`false`).\n\t * @default []\n\t */\n\toptions?: readonly string[] | boolean;\n\n\t/**\n\t * The prefixes for both flags and options.\n\t * @default ['--', '-', '—']\n\t */\n\tprefixes?: string[];\n\n\t/**\n\t * The flag separators.\n\t * @default ['=', ':']\n\t */\n\tseparators?: string[];\n}\n\nconst never = () => Option.none;\nconst always = () => true;\n\nexport class FlagUnorderedStrategy extends PrefixedStrategy {\n\tpublic readonly flags: readonly string[] | true;\n\tpublic readonly options: readonly string[] | true;\n\n\tpublic constructor({ flags, options, prefixes = ['--', '-', '—'], separators = ['=', ':'] }: FlagStrategyOptions = {}) {\n\t\tsuper(prefixes, separators);\n\t\tthis.flags = flags || [];\n\t\tthis.options = options || [];\n\n\t\tif (this.flags === true) this.allowedFlag = always;\n\t\telse if (this.flags.length === 0) this.matchFlag = never;\n\n\t\tif (this.options === true) {\n\t\t\tthis.allowedOption = always;\n\t\t} else if (this.options.length === 0) {\n\t\t\tthis.matchOption = never;\n\t\t}\n\t}\n\n\tpublic override matchFlag(s: string): Option<string> {\n\t\tconst result = super.matchFlag(s);\n\n\t\t// The flag must be an allowed one.\n\t\tif (result.isSomeAnd((value) => this.allowedFlag(value))) return result;\n\n\t\t// If it did not match a flag, return null.\n\t\treturn Option.none;\n\t}\n\n\tpublic override matchOption(s: string): Option<readonly [key: string, value: string]> {\n\t\tconst result = super.matchOption(s);\n\n\t\tif (result.isSomeAnd((option) => this.allowedOption(option[0]))) return result;\n\n\t\treturn Option.none;\n\t}\n\n\tprivate allowedFlag(s: string) {\n\t\treturn (this.flags as readonly string[]).includes(s);\n\t}\n\n\tprivate allowedOption(s: string) {\n\t\treturn (this.options as readonly string[]).includes(s);\n\t}\n}\n"],"mappings":";;;;;AAoCA,MAAM,cAAcA,yBAAO;AAC3B,MAAM,eAAe;AAErB,IAAa,wBAAb,cAA2CC,mCAAiB;CAI3D,AAAO,YAAY,EAAE,OAAO,SAAS,WAAW;EAAC;EAAM;EAAK;EAAI,EAAE,aAAa,CAAC,KAAK,IAAI,KAA0B,EAAE,EAAE;AACtH,QAAM,UAAU,WAAW;AAC3B,OAAK,QAAQ,SAAS,EAAE;AACxB,OAAK,UAAU,WAAW,EAAE;AAE5B,MAAI,KAAK,UAAU,KAAM,MAAK,cAAc;WACnC,KAAK,MAAM,WAAW,EAAG,MAAK,YAAY;AAEnD,MAAI,KAAK,YAAY,KACpB,MAAK,gBAAgB;WACX,KAAK,QAAQ,WAAW,EAClC,MAAK,cAAc;;CAIrB,AAAgB,UAAU,GAA2B;EACpD,MAAM,SAAS,MAAM,UAAU,EAAE;AAGjC,MAAI,OAAO,WAAW,UAAU,KAAK,YAAY,MAAM,CAAC,CAAE,QAAO;AAGjE,SAAOD,yBAAO;;CAGf,AAAgB,YAAY,GAA0D;EACrF,MAAM,SAAS,MAAM,YAAY,EAAE;AAEnC,MAAI,OAAO,WAAW,WAAW,KAAK,cAAc,OAAO,GAAG,CAAC,CAAE,QAAO;AAExE,SAAOA,yBAAO;;CAGf,AAAQ,YAAY,GAAW;AAC9B,SAAQ,KAAK,MAA4B,SAAS,EAAE;;CAGrD,AAAQ,cAAc,GAAW;AAChC,SAAQ,KAAK,QAA8B,SAAS,EAAE"}