MCPcopy Create free account
hub / github.com/Effect-TS/effect / defaultTransforms

Function defaultTransforms

packages/effect/src/unstable/sql/Statement.ts:1137–1206  ·  view source on GitHub ↗
(
  transformer: (str: string) => string,
  nested = true
)

Source from the content-addressed store, hash-verified

1135 * @since 4.0.0
1136 */
1137export const defaultTransforms = (
1138 transformer: (str: string) => string,
1139 nested = true
1140) => {
1141 const transformValue = (value: any) => {
1142 if (Array.isArray(value)) {
1143 if (value.length === 0 || value[0].constructor !== Object) {
1144 return value
1145 }
1146 return array(value)
1147 } else if (value?.constructor === Object) {
1148 return transformObject(value)
1149 }
1150 return value
1151 }
1152
1153 const transformObject = (obj: Record<string, any>): any => {
1154 const newObj: Record<string, any> = {}
1155 for (const key of Object.keys(obj)) {
1156 InternalRecord.assignProperty(newObj, transformer(key), transformValue(obj[key]))
1157 }
1158 return newObj
1159 }
1160
1161 const transformArrayNested = <A extends object>(
1162 rows: ReadonlyArray<A>
1163 ): ReadonlyArray<A> => {
1164 const newRows: Array<A> = new Array(rows.length)
1165 for (let i = 0, len = rows.length; i < len; i++) {
1166 const row = rows[i]
1167 if (Array.isArray(row)) {
1168 newRows[i] = transformArrayNested(row) as any
1169 } else {
1170 const obj: any = {}
1171 for (const [key, value] of Object.entries(row)) {
1172 InternalRecord.assignProperty(obj, transformer(key), transformValue(value))
1173 }
1174 newRows[i] = obj
1175 }
1176 }
1177 return newRows
1178 }
1179
1180 const transformArray = <A extends object>(
1181 rows: ReadonlyArray<A>
1182 ): ReadonlyArray<A> => {
1183 const newRows: Array<A> = new Array(rows.length)
1184 for (let i = 0, len = rows.length; i < len; i++) {
1185 const row = rows[i]
1186 if (Array.isArray(row)) {
1187 newRows[i] = transformArray(row) as any
1188 } else {
1189 const obj: any = {}
1190 for (const [key, value] of Object.entries(row)) {
1191 InternalRecord.assignProperty(obj, transformer(key), value)
1192 }
1193 newRows[i] = obj
1194 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected