MCPcopy Index your code
hub / github.com/Effect-TS/effect / optimizeSafe

Function optimizeSafe

packages/printer/src/internal/optimize.ts:13–141  ·  view source on GitHub ↗
(
  self: Doc.Doc<A>,
  depth: Optimize.Optimize.Depth
)

Source from the content-addressed store, hash-verified

11>(2, (self, depth) => Effect.runSync(optimizeSafe(self, depth)))
12
13const optimizeSafe = <A>(
14 self: Doc.Doc<A>,
15 depth: Optimize.Optimize.Depth
16): Effect.Effect<Doc.Doc<A>> => {
17 const optimize = (self: Doc.Doc<A>): Effect.Effect<Doc.Doc<A>> =>
18 Effect.gen(function*() {
19 switch (self._tag) {
20 case "Fail":
21 case "Empty":
22 case "Char":
23 case "Text":
24 case "Line": {
25 return self
26 }
27 case "FlatAlt": {
28 const left = yield* optimize(self.left)
29 const right = yield* optimize(self.right)
30 return InternalDoc.flatAlt(left, right)
31 }
32 case "Cat": {
33 // Empty Documents
34 if (InternalDoc.isEmpty(self.left)) {
35 return yield* optimize(self.right)
36 }
37 if (InternalDoc.isEmpty(self.right)) {
38 return yield* optimize(self.left)
39 }
40 // Text Documents
41 if (InternalDoc.isChar(self.left) && InternalDoc.isChar(self.right)) {
42 return InternalDoc.text(self.left.char + self.right.char)
43 }
44 if (InternalDoc.isText(self.left) && InternalDoc.isChar(self.right)) {
45 return InternalDoc.text(self.left.text + self.right.char)
46 }
47 if (InternalDoc.isChar(self.left) && InternalDoc.isText(self.right)) {
48 return InternalDoc.text(self.left.char + self.right.text)
49 }
50 if (InternalDoc.isText(self.left) && InternalDoc.isText(self.right)) {
51 return InternalDoc.text(self.left.text + self.right.text)
52 }
53 // Nested Text Documents
54 if (
55 (
56 InternalDoc.isChar(self.left) &&
57 InternalDoc.isCat(self.right) &&
58 InternalDoc.isChar(self.right.left)
59 ) ||
60 (
61 InternalDoc.isChar(self.left) &&
62 InternalDoc.isCat(self.right) &&
63 InternalDoc.isText(self.right.left)
64 ) ||
65 (
66 InternalDoc.isText(self.left) &&
67 InternalDoc.isCat(self.right) &&
68 InternalDoc.isChar(self.right.left)
69 ) ||
70 (

Callers 2

optimize.tsFile · 0.85
optimizeFunction · 0.85

Calls 1

optimizeFunction · 0.70

Tested by

no test coverage detected