> ## Documentation Index
> Fetch the complete documentation index at: https://docs.js-confuser.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Flatten

> Brings independent declarations to the highest scope.

* Option name: `"flatten"`

* Option values: `true/false/0-1/Function`

***

### Input / Output

This example showcases how `Flatten` transforms the code. Try it out by changing the input code and see changes apply in real-time.

```js title="Input.js" lines interactive-mode="obfuscate" theme={null}
// Input.js
(function () {
  var stringToPrint = 'Hello World';
  var timesPrinted = 0;

  function printString() {
    timesPrinted++;
    console.log(stringToPrint);
  }

  printString(); // "Hello World"
})();

// Output.js
function X8ogav(X8ogav, []) {
  var dNSdKA = 'Hello World',
    AEUaj0 = 0;
  function dHVaMLd(...dHVaMLd) {
    var gSoXcW = {
      get bdQBDF() {
        return AEUaj0;
      },
      set bdQBDF(dHVaMLd) {
        AEUaj0 = dHVaMLd;
      },
      get msGdVV() {
        return dNSdKA;
      },
    };
    return X8ogav.HOzLZRO(gSoXcW, dHVaMLd);
  }
  dHVaMLd();
}
function dNSdKA(X8ogav, []) {
  X8ogav.bdQBDF++;
  console.log(X8ogav.msGdVV);
}
(function (...NZDJP1) {
  var CTgisrD = {
    HOzLZRO(...NZDJP1) {
      return dNSdKA(...NZDJP1);
    },
  };
  return X8ogav(CTgisrD, NZDJP1);
})();
```

***

### Custom Implementation

#### `options.flatten(fnName)`

Control which functions are changed. Returns a `boolean`.

| Parameter | Type     | Description                               |
| --------- | -------- | ----------------------------------------- |
| `fnName`  | `string` | The function name proposed to be changed. |

***

### Usage Example

The provided code example will obfuscate the file `input.js` and write the output to a file named `output.js`.

```js title="Usage Example" lines theme={null}
import JSConfuser from "js-confuser";
import {readFileSync, writeFileSync} from "fs";

// Read input code
const sourceCode = readFileSync("input.js", "utf8");
const options = {
  target: 'browser',
  flatten: true,
};

JSConfuser.obfuscate(sourceCode, options).then((result)=>{
  // Write output code
  writeFileSync("output.js", result.code);
}).catch(err=>{
  // Error occurred
  console.error(err);  
});
```

***

#### Enabled In

* [High Preset](/presets/high): Yes
* [Medium Preset](/presets/medium): No
* [Low Preset](/presets/low): No
