{"id":11187,"library":"jsprim","title":"jsprim: JavaScript Primitive Utilities","description":"jsprim is a utility library focused on fundamental operations for JavaScript primitive types and common object manipulations. It provides functions for deep cloning, thorough deep equality comparisons, and efficient inspection/transformation of object structures, including an O(1) `isEmpty` check and memory-efficient `flattenIter` for traversing deeply nested objects without materializing the full result array. The current stable version is 2.0.2, with the last update approximately four years ago. It targets older Node.js environments (originally requiring Node.js >=0.6.0) and uses the CommonJS module system, making it suitable for legacy projects or environments where explicit CJS interop is managed.","status":"maintenance","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/joyent/node-jsprim","tags":["javascript"],"install":[{"cmd":"npm install jsprim","lang":"bash","label":"npm"},{"cmd":"yarn add jsprim","lang":"bash","label":"yarn"},{"cmd":"pnpm add jsprim","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"jsprim is a CommonJS module. Use `require()` to import the entire module object.","wrong":"import jsprim from 'jsprim';","symbol":"jsprim","correct":"const jsprim = require('jsprim');"},{"note":"For named functions, destructure from the `require()` result. Direct ES module named imports are not supported.","wrong":"import { deepCopy } from 'jsprim';","symbol":"deepCopy","correct":"const { deepCopy } = require('jsprim');"},{"note":"As a CommonJS module, named exports are accessed by destructuring the `require()` result. ES module named imports will fail.","wrong":"import { isEmpty } from 'jsprim';","symbol":"isEmpty","correct":"const { isEmpty } = require('jsprim');"}],"quickstart":{"code":"const jsprim = require('jsprim');\n\n// Example 1: Deep Copy\nconst originalObject = { a: 1, b: { c: 2 }, d: [3, 4] };\nconst copiedObject = jsprim.deepCopy(originalObject);\nconsole.log('Deep Copy:', copiedObject);\nconsole.log('Are copies identical? (reference check):', originalObject === copiedObject); // Should be false\nconsole.log('Are contents deeply equal:', jsprim.deepEqual(originalObject, copiedObject)); // Should be true\n\n// Example 2: Deep Equal\nconst obj1 = { x: 1, y: { z: 2 } };\nconst obj2 = { x: 1, y: { z: 2 } };\nconst obj3 = { x: 1, y: { z: 3 } };\nconsole.log('Deep Equal (obj1, obj2):', jsprim.deepEqual(obj1, obj2)); // Should be true\nconsole.log('Deep Equal (obj1, obj3):', jsprim.deepEqual(obj1, obj3)); // Should be false\n\n// Example 3: Is Empty\nconsole.log('Is {} empty?', jsprim.isEmpty({})); // Should be true\nconsole.log('Is {a: 1} empty?', jsprim.isEmpty({ a: 1 })); // Should be false\n\n// Example 4: Pluck\nconst nestedObject = {\n    user: {\n        profile: {\n            name: 'Alice',\n            age: 30\n        },\n        settings: { theme: 'dark' }\n    },\n    'user.id': '123'\n};\nconsole.log('Pluck user.profile.name:', jsprim.pluck(nestedObject, 'user.profile.name')); // Should be 'Alice'\nconsole.log('Pluck user.id:', jsprim.pluck(nestedObject, 'user.id')); // Should be '123' (direct property takes precedence)\nconsole.log('Pluck nonExistent.path:', jsprim.pluck(nestedObject, 'nonExistent.path')); // Should be undefined\n\n// Example 5: flattenIter (demonstrates use without full array materialization)\nconst data = {\n    'RegionA': { 'CityX': { 'pop': 100 }, 'CityY': { 'pop': 200 } },\n    'RegionB': { 'CityZ': { 'pop': 300 } }\n};\nconst flattenedResults = [];\njsprim.flattenIter(data, 2, (entry) => {\n    flattenedResults.push(entry);\n});\nconsole.log('Flattened results (using flattenIter):', flattenedResults);\n","lang":"javascript","description":"Demonstrates core utilities like deepCopy, deepEqual, isEmpty, pluck for nested property access, and flattenIter for efficient object traversal without materializing full arrays."},"warnings":[{"fix":"Be mindful of the `depth === 0` edge case when using `flattenObject`. If consistent iteration behavior is required, especially for `depth === 0`, `flattenIter` might be a more predictable choice, or ensure explicit handling for the `flattenObject` output when `depth` is 0.","message":"The `flattenObject` and `flattenIter` functions exhibit a behavioral difference when the `depth` parameter is 0. `flattenObject` omits the array wrapping the original object in this specific case, while `flattenIter`'s behavior is logically equivalent to `flattenObject(obj, depth).forEach(func)`, which implies a consistent array-like iteration.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure you are using `require()` for importing `jsprim` in Node.js environments. If using in an ES module context, you may need to use `import * as jsprim from 'jsprim'` or configure your build system (e.g., Babel, webpack) for CommonJS interop.","message":"This package is a CommonJS module and does not natively support ES module `import` syntax. Attempting to use `import jsprim from 'jsprim'` or `import { someFunc } from 'jsprim'` directly in an ES module environment will result in errors without proper interop configuration or a build step.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate the library's functionality against your specific project requirements and Node.js version. Consider auditing its source if used in security-sensitive contexts, or look for more actively maintained alternatives if up-to-date features and support are critical.","message":"The library has not been updated in approximately four years and targets Node.js versions as old as 0.6.0. While functional, it may not leverage modern JavaScript features, performance improvements, or security best practices introduced in newer Node.js releases.","severity":"gotcha","affected_versions":"<=2.0.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use CommonJS destructuring: `const { deepCopy } = require('jsprim');`","cause":"Attempting to use ES module named import syntax for `jsprim`, which is a CommonJS module.","error":"SyntaxError: Named export 'deepCopy' not found. The requested module 'jsprim' does not provide an export named 'deepCopy'"},{"fix":"If `jsprim` is the only CJS dependency, consider `import * as jsprim from 'jsprim';` if you're in an ES module context. Otherwise, ensure your file is treated as CommonJS (e.g., by using `.cjs` extension or `\"type\": \"commonjs\"` in package.json).","cause":"Attempting to use `require()` in an ES module file where `type: \"module\"` is set in package.json, or implicitly when Node.js treats a file as ESM.","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Ensure the `depth` argument for `flattenObject` or `flattenIter` is always a non-negative integer (0 or greater).","cause":"Providing an invalid `depth` parameter (e.g., negative number, non-integer) to `flattenObject` or `flattenIter`.","error":"Error: \"depth\" must be a non-negative integer"}],"ecosystem":"npm"}