{"_id":"@alfarizi/convert-undefined-null","name":"@alfarizi/convert-undefined-null","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@alfarizi/convert-undefined-null","version":"1.0.0","description":"Convert object that contain undefined to null and convert object that contain null to undefined","main":"./dist/index.js","module":"./dist/index.mjs","types":"./dist/index.d.ts","scripts":{"build":"tsup","test":"jest"},"repository":{"type":"git","url":"git+https://github.com/alfariiizi/convert-undefined-null.git"},"keywords":["convert","object","undefined","null"],"author":{"name":"Moh Rizal Alfarizi"},"license":"MIT","bugs":{"url":"https://github.com/alfariiizi/convert-to-number/issues"},"homepage":"https://github.com/alfariiizi/convert-to-number#readme","devDependencies":{"@types/jest":"^29.5.14","jest":"^29.7.0","ts-jest":"^29.2.5","ts-node":"^10.9.2","tsup":"^8.3.5","typescript":"^5.7.2"},"_id":"@alfarizi/convert-undefined-null@1.0.0","gitHead":"cd865136275940f2f67fe89ec932f85b10e08951","_nodeVersion":"20.11.1","_npmVersion":"10.8.2","dist":{"integrity":"sha512-sU0bhLZ1JWs0bvDLqlJKi3nn0hKbzooviaYTeOV0yR1tdEQ1rSq843kGzfSvi5u7BnFP3/xI0RqfhN8xFDaysQ==","shasum":"a979e821f84fa57c27f4d92d229abac6518cd97f","tarball":"https://registry.npmjs.org/@alfarizi/convert-undefined-null/-/convert-undefined-null-1.0.0.tgz","fileCount":9,"unpackedSize":21330,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCz2cjf5xAUNLNJXsf4VZguEZ8SNwLPuXo+SjqeczQ+hQIgNXlkpWfSoCdzBthgU6NV9aq9GIeu+HdV4tpi/PhLnN0="}]},"_npmUser":{"name":"alfariiizi","email":"alfariiiziiiii@gmail.com"},"directories":{},"maintainers":[{"name":"alfariiizi","email":"alfariiiziiiii@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/convert-undefined-null_1.0.0_1735452872101_0.254519705396836"},"_hasShrinkwrap":false}},"time":{"created":"2024-12-29T06:14:32.015Z","1.0.0":"2024-12-29T06:14:32.284Z","modified":"2024-12-29T06:14:32.551Z"},"maintainers":[{"name":"alfariiizi","email":"alfariiiziiiii@gmail.com"}],"description":"Convert object that contain undefined to null and convert object that contain null to undefined","homepage":"https://github.com/alfariiizi/convert-to-number#readme","keywords":["convert","object","undefined","null"],"repository":{"type":"git","url":"git+https://github.com/alfariiizi/convert-undefined-null.git"},"author":{"name":"Moh Rizal Alfarizi"},"bugs":{"url":"https://github.com/alfariiizi/convert-to-number/issues"},"license":"MIT","readme":"# @alfarizi/convert-undefined-null\n\n`ConvertNullUndefined` is a lightweight utility package that provides functions\nto transform `null` values to `undefined` and vice versa in JavaScript and\nTypeScript objects. This is especially useful when working with APIs or data\ntransformations where the distinction between `null` and `undefined` matters.\n\n## Installation\n\n```bash\nnpm install @alfarizi/convert-undefined-null\n```\n\nor\n\n```bash\nyarn add @alfarizi/convert-undefined-null\n```\n\n## Features\n\n- **Convert `null` values to `undefined`**: Use `convertNullToUndefined` to\n  recursively replace `null` values in objects, arrays, or primitives with\n  `undefined`.\n- **Convert `undefined` values to `null`**: Use `convertUndefinedToNull` to\n  recursively replace `undefined` values in objects, arrays, or primitives with\n  `null`.\n- Fully type-safe: Includes TypeScript support with accurate typings for input\n  and output transformations.\n- Handles nested structures, arrays, and edge cases seamlessly.\n\n## Usage\n\n### Importing\n\n```typescript\nimport convertNullToUndefined from \"@alfarizi/convert-undefined-null\";\nimport convertUndefinedToNull from \"@alfarizi/convert-undefined-null\";\n```\n\n### Converting `null` to `undefined`\n\n```typescript\ntype SampleType = {\n  name: string | null;\n  age: number | null;\n  tags: (string | null)[];\n};\n\nconst sample: SampleType = {\n  name: null,\n  age: 25,\n  tags: [\"tag1\", null, \"tag3\"],\n};\n\nconst result = convertNullToUndefined(sample);\n\nconsole.log(result);\n// Output: { name: undefined, age: 25, tags: [\"tag1\", undefined, \"tag3\"] }\n```\n\n### Converting `undefined` to `null`\n\n```typescript\ntype SampleType = {\n  name: string | undefined;\n  age: number | undefined;\n  tags: (string | undefined)[];\n};\n\nconst sample: SampleType = {\n  name: undefined,\n  age: 25,\n  tags: [\"tag1\", undefined, \"tag3\"],\n};\n\nconst result = convertUndefinedToNull(sample);\n\nconsole.log(result);\n// Output: { name: null, age: 25, tags: [\"tag1\", null, \"tag3\"] }\n```\n\n### Handling Edge Cases\n\n- Empty objects and arrays are returned as-is.\n- Non-object values are converted directly (e.g., `null` to `undefined` or\n  `undefined` to `null`).\n- Works seamlessly with nested objects and arrays.\n\n## API\n\n### `convertNullToUndefined`\n\n```typescript\nfunction convertNullToUndefined<T extends object | undefined>(\n  obj: T,\n): T extends object ? ReplaceNullWithUndefined<T> : undefined;\n```\n\n#### Parameters\n\n- `obj`: The object, array, or primitive value to convert.\n\n#### Returns\n\n- A new object with all `null` values replaced by `undefined`.\n\n### `convertUndefinedToNull`\n\n```typescript\nfunction convertUndefinedToNull<T extends object | undefined>(\n  obj: T,\n): T extends object ? ReplaceUndefinedWithNull<T> : null;\n```\n\n#### Parameters\n\n- `obj`: The object, array, or primitive value to convert.\n\n#### Returns\n\n- A new object with all `undefined` values replaced by `null`.\n\n## Type Definitions\n\n### `ReplaceNullWithUndefined`\n\nTransforms all `null` values in a type to `undefined`.\n\n```typescript\ntype ReplaceNullWithUndefined<T> = {\n  [K in keyof T]: T[K] extends (infer U)[]\n    ? ReplaceNullWithUndefined<U>[]\n    : T[K] extends object\n      ? ReplaceNullWithUndefined<T[K]>\n      : Exclude<T[K], null | \"\"> | undefined;\n};\n```\n\n### `ReplaceUndefinedWithNull`\n\nTransforms all `undefined` values in a type to `null`.\n\n```typescript\ntype ReplaceUndefinedWithNull<T> = {\n  [K in keyof T]: T[K] extends (infer U)[]\n    ? ReplaceUndefinedWithNull<U>[]\n    : T[K] extends object\n      ? ReplaceUndefinedWithNull<T[K]>\n      : Exclude<T[K], undefined> | null;\n};\n```\n\n## Contributing\n\nContributions are welcome! Feel free to open an issue or submit a pull request.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file\nfor details.\n","readmeFilename":"README.md"}