{"size": 2537, "ext": "js", "max_stars_repo_path": "examples/versions.js", "max_stars_repo_name": "devinea/json-ptr", "max_stars_repo_stars_event_min_datetime": "2015-01-20T07:06:35.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T13:06:18.000Z", "max_issues_repo_path": "examples/versions.js", "max_issues_repo_name": "devinea/json-ptr", "max_issues_repo_issues_event_min_datetime": "2015-07-08T15:21:06.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-07T21:59:09.000Z", "max_forks_repo_path": "examples/versions.js", "max_forks_repo_name": "devinea/json-ptr", "max_forks_repo_forks_event_min_datetime": "2015-03-20T06:06:16.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-26T14:41:46.000Z"}
{"max_stars_count": 70, "max_issues_count": 33, "max_forks_count": 27, "avg_line_length": 27.5760869565, "max_line_length": 83, "alphanum_fraction": 0.5340953883}
/**
 * @hidden
 * @packageDocumentation
 */
import { JsonPointer } from '../dist';
const versions = {
    '1.0': {
        name: JsonPointer.create('/guests/0/name'),
        surname: JsonPointer.create('/guests/0/surname'),
        honorific: JsonPointer.create('/guests/0/honorific'),
    },
    '1.1': {
        name: JsonPointer.create('/primary/primaryGuest/name'),
        surname: JsonPointer.create('/primary/primaryGuest/surname'),
        honorific: JsonPointer.create('/primary/primaryGuest/honorific'),
    },
};
/**
 * Gets the primary guest's name from the specified reservation.
 * @param reservation a reservation, either version 1.0 or bearing a `version`
 * property indicating the version.
 */
function primaryGuestName(reservation) {
    const pointers = versions[reservation.version || '1.0'];
    if (!pointers) {
        throw new Error(`Unsupported reservation version: ${reservation.version}`);
    }
    const name = pointers.name.get(reservation);
    const surname = pointers.surname.get(reservation);
    const honorific = pointers.honorific.get(reservation);
    const names = [];
    if (honorific)
        names.push(honorific);
    if (name)
        names.push(name);
    if (surname)
        names.push(surname);
    return names.join(' ');
}
// The original layout of a reservation (only the parts relevant to our example)
const reservationV1 = {
    guests: [
        {
            name: 'Wilbur',
            surname: 'Finkle',
            honorific: 'Mr.',
        },
        {
            name: 'Wanda',
            surname: 'Finkle',
            honorific: 'Mrs.',
        },
        {
            name: 'Wilma',
            surname: 'Finkle',
            honorific: 'Miss',
            child: true,
            age: 12,
        },
    ],
    // ...
};
// The new layout of a reservation (only the parts relevant to our example)
const reservationV1_1 = {
    version: '1.1',
    primary: {
        primaryGuest: {
            name: 'Wilbur',
            surname: 'Finkle',
            honorific: 'Mr.',
        },
        additionalGuests: [
            {
                name: 'Wanda',
                surname: 'Finkle',
                honorific: 'Mrs.',
            },
            {
                name: 'Wilma',
                surname: 'Finkle',
                honorific: 'Miss',
                child: true,
                age: 12,
            },
        ],
        // ...
    },
    // ...
};
console.log(primaryGuestName(reservationV1));
console.log(primaryGuestName(reservationV1_1));
