|
| 1 | +/*-------------------------------------------------------------------------- |
| 2 | +
|
| 3 | +@sinclair/parsebox |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) 2024 Haydn Paterson (sinclair) (haydn.developer@gmail.com) |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +
|
| 27 | +---------------------------------------------------------------------------*/ |
| 28 | + |
| 29 | +import { Runtime } from '@sinclair/parsebox' |
| 30 | + |
| 31 | +// ----------------------------------------------------------------------- |
| 32 | +// Json |
| 33 | +// ----------------------------------------------------------------------- |
| 34 | +const Json = Runtime.Union([ |
| 35 | + Runtime.Ref('Number'), |
| 36 | + Runtime.Ref('Boolean'), |
| 37 | + Runtime.Ref('String'), |
| 38 | + Runtime.Ref('Null'), |
| 39 | + Runtime.Ref('Object'), |
| 40 | + Runtime.Ref('Array') |
| 41 | +]) |
| 42 | +// ----------------------------------------------------------------------- |
| 43 | +// Number |
| 44 | +// ----------------------------------------------------------------------- |
| 45 | +function NumberMapping(value: string) { |
| 46 | + return parseFloat(value) |
| 47 | +} |
| 48 | +const Number = Runtime.Number(NumberMapping) |
| 49 | +// ----------------------------------------------------------------------- |
| 50 | +// String |
| 51 | +// ----------------------------------------------------------------------- |
| 52 | +function StringMapping(value: string) { |
| 53 | + return value |
| 54 | +} |
| 55 | +const String = Runtime.String(['"'], StringMapping) |
| 56 | +// ----------------------------------------------------------------------- |
| 57 | +// Boolean |
| 58 | +// ----------------------------------------------------------------------- |
| 59 | +function BooleanMapping(value: string) { |
| 60 | + return value === 'true' |
| 61 | +} |
| 62 | +const Boolean = Runtime.Union([ |
| 63 | + Runtime.Const('true'), |
| 64 | + Runtime.Const('false'), |
| 65 | +], BooleanMapping) |
| 66 | + |
| 67 | +// ----------------------------------------------------------------------- |
| 68 | +// Null |
| 69 | +// ----------------------------------------------------------------------- |
| 70 | +function NullMapping(_value: string) { |
| 71 | + return null |
| 72 | +} |
| 73 | +const Null = Runtime.Const('null', NullMapping) |
| 74 | +// ----------------------------------------------------------------------- |
| 75 | +// Property |
| 76 | +// ----------------------------------------------------------------------- |
| 77 | +const Key = Runtime.Union([Runtime.String(['"'])]) |
| 78 | + |
| 79 | +function PropertyMapping(values: unknown[]) { |
| 80 | + return { [values[0] as string]: values[2] } |
| 81 | +} |
| 82 | +const Property = Runtime.Tuple([Runtime.Ref('Key'), Runtime.Const(':'), Runtime.Ref('Json')], PropertyMapping) |
| 83 | + |
| 84 | +// ----------------------------------------------------------------------- |
| 85 | +// Properties |
| 86 | +// ----------------------------------------------------------------------- |
| 87 | +function PropertiesMapping(values: unknown[]) { |
| 88 | + return ( |
| 89 | + values.length === 3 ? [values[0], ...values[2] as unknown[]] : |
| 90 | + values.length === 1 ? [values[0]] : |
| 91 | + [] |
| 92 | + ) |
| 93 | +} |
| 94 | +const Properties = Runtime.Union([ |
| 95 | + Runtime.Tuple([Runtime.Ref('Property'), Runtime.Const(','), Runtime.Ref('Properties')]), |
| 96 | + Runtime.Tuple([Runtime.Ref('Property')]), |
| 97 | + Runtime.Tuple([]) |
| 98 | +], PropertiesMapping) |
| 99 | +// ----------------------------------------------------------------------- |
| 100 | +// Object |
| 101 | +// ----------------------------------------------------------------------- |
| 102 | +function ObjectReduce(propertiesList: Record<PropertyKey, unknown>[]) { |
| 103 | + return propertiesList.reduce((result, properties) => { |
| 104 | + return {...result, ...properties } |
| 105 | + }, {}) |
| 106 | +} |
| 107 | +function ObjectMapping(values: unknown[]) { |
| 108 | + return ObjectReduce(values[1] as Record<PropertyKey, unknown>[]) |
| 109 | +} |
| 110 | +const _Object = Runtime.Tuple([ |
| 111 | + Runtime.Const('{'), |
| 112 | + Runtime.Ref('Properties'), |
| 113 | + Runtime.Const('}') |
| 114 | +], ObjectMapping) |
| 115 | +// ----------------------------------------------------------------------- |
| 116 | +// Elemments |
| 117 | +// ----------------------------------------------------------------------- |
| 118 | +function ElementsMapping(values: unknown[]) { |
| 119 | + return ( |
| 120 | + values.length === 3 ? [values[0], ...values[2] as unknown[]] : |
| 121 | + values.length === 1 ? [values[0]] : |
| 122 | + [] |
| 123 | + ) |
| 124 | +} |
| 125 | +const Elements = Runtime.Union([ |
| 126 | + Runtime.Tuple([Runtime.Ref('Json'), Runtime.Const(','), Runtime.Ref('Elements')]), |
| 127 | + Runtime.Tuple([Runtime.Ref('Json')]), |
| 128 | + Runtime.Tuple([]) |
| 129 | +], ElementsMapping) |
| 130 | +// ----------------------------------------------------------------------- |
| 131 | +// Array |
| 132 | +// ----------------------------------------------------------------------- |
| 133 | +function ArrayMapping(values: unknown[]) { |
| 134 | + return values[1] |
| 135 | +} |
| 136 | +const Array = Runtime.Tuple([Runtime.Const('['), Elements, Runtime.Const(']')], ArrayMapping) |
| 137 | + |
| 138 | +export const Module = new Runtime.Module({ |
| 139 | + Number, |
| 140 | + Boolean, |
| 141 | + String, |
| 142 | + Null, |
| 143 | + Key, |
| 144 | + Property, |
| 145 | + Properties, |
| 146 | + Object: _Object, |
| 147 | + Elements, |
| 148 | + Array, |
| 149 | + Json |
| 150 | +}) |
0 commit comments