Skip to content

fix has and use undefined instead of null#147

Open
Mara-Li wants to merge 6 commits intoeslachance:mainfrom
Dicelette:dicelette
Open

fix has and use undefined instead of null#147
Mara-Li wants to merge 6 commits intoeslachance:mainfrom
Dicelette:dicelette

Conversation

@Mara-Li
Copy link

@Mara-Li Mara-Li commented Jan 26, 2026

As discussed on the discord:

  1. Before 6.1.5, the library return undefined when a value was not found/present. I created my bots (I use enmap on each! Great works!) around this because I use a lot optional (x?: string) that leads to strictly undefined. Typescript doesn't allow to set undefined = null like in Javascript. It leads to a breaking change in my base so... I changed the value returned.
  2. has doesn't work in 6.1.5 and path was not accepted:
import Enmap from "enmap";

interface MyServerData {
    language: string;
    welcomeMessage: string;
    otherSettings?: {
        moderator: string;
        notificationsEnabled: boolean;
    }
}

const database: Enmap<MyServerData> = new Enmap({
    name: "myServerData",
});

database.set("server123", {
    language: "en",
    welcomeMessage: "Welcome to the server!",
    otherSettings: {
        moderator: "adminUser",
        notificationsEnabled: true,
    }
})

database.set("server123", "fr", "language");
const key = database.get("server123", "language");
const otherSettings = database.get("server123", "otherSettings.moderator");
const welcomeMessage = database.get("server123", "welcomeMessage");
const k = database.has("server123.welcomeMessage");
console.log(`Welcome Message: ${welcomeMessage} | Present: ${k}`);

Result: Welcome Message: Welcome to the server! | Present: false

@Mara-Li Mara-Li changed the title Dicelette fix has and use undefined instead of null Jan 27, 2026
@miichom
Copy link
Contributor

miichom commented Jan 28, 2026

I noticed the the has test method is missing the path, here's something I quickly did up.

describe('has', () => {
  const enmap = new Enmap({ inMemory: true });
  
  test('should return true if key exists', () => {
    enmap.set('has', 'value');
    
    expect(enmap.has('has')).toBe(true);
  });
  
+  test('should return true if key and path exists', () => {
+    enmap.set('hasPath', 'value', 'nested');
+    
+    expect(enmap.has('hasPath', 'nested')).toBe(true);
+  });
  
  test("should return false if key doesn't exist", () => {
    expect(enmap.has('unknown')).toBe(false);
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants