npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gedcomx-js

v2.8.0

Published

JavaScript object model for GEDCOM X

Downloads

1,233

Readme

GEDCOM X JS

npm Build Status codecov Scrutinizer Code Quality

A JavaScript library for the GEDCOM X data model including the GEDCOM X RS, the GEDCOM X Records, and the GEDCOM X Atom Extensions specifications.

Read the documentation for a list of all classes and methods.

Install

NPM

npm install --save gedcomx-js

CDN via unpkg

<script src="https://unpkg.com/[email protected]/dist/gedcomx.min.js"></script>

In the browser, gedcomx-js is attached to the window as a global named GedcomX.

Usage

Create a document from a JSON object.

var doc = new GedcomX({
  persons: [
    {
      id: 'person1',
      facts: [
        {
          type: 'http://gedcomx.org/Birth',
          date: {
            formal: '+2014-04-09'
          },
          place: {
            original: 'Verona'
          }
        }
      ]
    }
  ]
});

Or build a document from scratch.

var doc = new GedcomX()
  .addPerson(
    new GedcomX.Person()
      .addFact(
        new GedcomX.Fact()
          .setType('http://gedcomx.org/Birth')
          .setDate(new GedcomX.Date().setFormal('+2014-04-09'))
          .setPlace(new GedcomX.PlaceReference().setOriginal('Verona'))
      )
  );

Or mix and match.

var doc = new GedcomX({
  persons: [
    new GedcomX.Person({
      facts: [
        new GedcomX.Fact()
          .setType('http://gedcomx.org/Birth')
          .setDate({ formal: '+2014-04-09' })
          .setPlace({ original: 'Verona' })
      ]
    })
  ]
});

RS Spec, Records Spec, and Atom Extensions

The RS, Records, and Atom extensions are disabled by default. They must be explicitly enabled.

GedcomX.enableRsExtensions();
GedcomX.enableRecordsExtensions();
GedcomX.enableAtomExtensions();

Note that the Atom extensions depend on the RS extensions thus calling enableAtomExtensions() will also call enableRsExtensions().

Installation

npm install gedcomx-js

Notes

The use of new when constructing objects is optional. The two following lines of code are equivalent:

new GedcomX.Person();
GedcomX.Person();

All objects have a toJSON() method for serialization.

The library is currently just an anemic domain model for the GEDCOM X JSON Serialization Format. We plan on eventually adding useful helpers and better enforcement of the specification (such as checking the format of formal dates).

You can add helpers of your own. Perhaps you're working with a data set where people only have one name with no prefix or suffix. You'll end up doing this often:

person.addName(
  GedcomX.Name()
    .addNameForm(
      GedcomX.NameForm()
        .addNamePart(
          GedcomX.NamePart()
            .setType('http://gedcomx.org/Given')
            .setValue('Jonathan')
          )
        .addNamePart(
          GedcomX.NamePart()
            .setType('http://gedcomx.org/Surname')
            .setValue('Burrows')
        )
    )
);

That's a lot of work for one name. So you could create your own helper method by adding to the GedcomX.Person prototype.

GedcomX.Person.prototype.addSimpleName = function(given, surname){
  this.addName(
    GedcomX.Name()
      .addNameForm(
        GedcomX.NameForm()
          .addNamePart(
            GedcomX.NamePart()
              .setType('http://gedcomx.org/Given')
              .setValue(given)
          )
          .addNamePart(
            GedcomX.NamePart()
              .setType('http://gedcomx.org/Surname')
              .setValue(surname)
          )
      )
  )
};

Then adding names is easy:

person.addSimpleName('Jonathan', 'Burrows');

Data Model Extensions

GEDCOM X allows for it's data model to be extended. The RS and Records specs are defined as extensions.

There are two ways that the spec can be extended.

1. Adding Properties to Existing Data Types

Extensions can add properties to existing data types. For example the RS spec defines resourceId as an extension to ResourceReference. gedcomx-js supports property extensions by using prototypical inheritance which allows for prototypes to be modified.

There are three situations where extensions need to be accounted for:

  1. Deserialization and Instantiation: All classes have an init() method which can me overriden to account for additional properties.

    // Override init() so that we can deserialize normalized values
    var oldInit = GedcomX.ResourceReference.prototype.init;
    GedcomX.ResourceReference.prototype.init = function(json){
      oldInit.call(this, json);
      if(json){
        this.setResourceId(json.resourceId);
      }
    };
  2. Getters and Setters: Just extend the prototype.

    /**
     * Set the resourceId
     * 
     * @param {Boolean} resourceId
     * @return {ResourceReference} this
     */
    GedcomX.ResourceReference.prototype.setResourceId = function(resourceId){
      this.resourceId = resourceId;
      return this;
    };
       
    /**
     * Get the resourceId
     * 
     * @return {Boolean} resourceId
     */
    GedcomX.ResourceReference.prototype.getResourceId = function(){
      return this.resourceId;
    };
  3. Serialization: Each class has a static jsonProps attribute which is a list of properties that should be serialized.

    // Extend serialization properties
    GedcomX.ResourceReference.jsonProps.push('resourceId');

    Not only does it support extensibility but it also great reduces code duplication.

2. Adding New Data Types

New data types are added by setting a reference on the exported GedcomX object.

var DisplayProperties = function(json){
  // Constructor
};

// Lots of prototype setup...

// Add to the GedcomX object
GedcomX.DisplayProperties = DisplayProperties;

Next we need to configure when our new data type will be used. The example above uses DisplayProperties which is defined in the RS spec as being an extension of Person. Thus we also must follow the method described above for adding the new property display to the existing data type Person.