Skip to content
This repository was archived by the owner on Apr 27, 2024. It is now read-only.

Add register once function #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdevine49
Copy link
Contributor

No description provided.

* @returns {void}
*/
function register_once(data) {
unpersistedData = Object.keys(data = {}).reduce(function (acc, key) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to understand what's happening here... could this be refactored to improve readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to only register data that hasn't already been registered. The reduce is building a new object out of the data based on which keys haven't been added to properties already. Then I'm registering that new object using the regular register function. To me, this is the clearest way to do that, but if you have a preference for another style I can try changing it.

Copy link
Owner

@john-doherty john-doherty Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, a "bit" late to this. Chat GPT suggested the following (based on your code):

/**
 * Register a set of super properties unless they are already added.
 * @param {object} data - JSON key/value pair of properties to register.
 * @returns {void}
 */
function register_once(data) {
    var unpersistedData = {};

    // Check each property in the input data
    Object.keys(data).forEach(function(key) {
        // If the property does not exist in _properties, add it to unpersistedData
        if (!_properties[key]) {
            unpersistedData[key] = data[key];
        }
    });

    // Register the new properties that were not already present
    register(unpersistedData);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants