Manual Reference Source Test

src/modules/identity-property.js

/**
 * Identity property configuration
 *
 * @property {string} label - Label to display next to property input
 * @property {string} detail - Detail text to display in small font next to property input
 * @property {string} placeholder - Placeholder value to show in input when empty
 * @property {string} type - Value type, one of: string/number/boolean or special type
 * @property {string|number|boolean} value - Initial input value (type should match)
 * @property {boolean} request - Whether to request this property during authentication
 * @property {boolean} required - Whether input value is required
 * @property {number} min - Minimum string length / number value
 * @property {number} max - Maximum string length / number value
 * @property {string} pattern - Regular expression that string should match
 */
class IdentityProperty {
  /**
   * Keys of credential properties:
   * - email: Email
   * - facebookId: Facebook user id
   * - id: IDM user id
   * - tcDate: Terms and conditions agreement date (number: milliseconds since epoch)
   * - username: Username
   *
   * Note: These don't have configurable labels etc. but they can be requested and read
   */
  static get credentialKeys() {
    return [
      'email',
      'facebookId',
      'id',
      'tcDate',
      'username',
    ];
  }

  /**
   * Standard identity properties:
   * - address: Address (line 1)
   * - address2: Address (line 2)
   * - birthday: Birthday
   * - country: Country
   * - firstName: First (a.k.a: given) name
   * - gender: Gender (e.g: male/female/other?)
   * - lastName: Last name (a.k.a: surname)
   * - phone: Phone
   * - state: State
   * - zip: Zip code
   *
   * @type {Map<string, IdentityProperty>}
   *
   * @return {Map<string, string>}
   */
  static get standardMap() {
    return {
      address: {
        label: 'Address (line 1)',
      },
      address2: {
        label: 'Address (line 2)',
      },
      birthday: {
        label: 'Birthday',
        type: 'date',
      },
      country: {
        label: 'Country',
      },
      firstName: {
        label: 'First name',
      },
      gender: {
        label: 'Gender',
      },
      lastName: {
        label: 'Last name',
      },
      phone: {
        label: 'Telephone number',
        type: 'tel',
      },
      state: {
        label: 'State',
      },
      zip: {
        label: 'Zip Code',
      },
      zipCode: {
        label: 'Zip Code',
      },
      dob: {
        label: 'Date of Birth',
      },
    };
  }

  /**
   * IdentityProperty constructor
   *
   * @param {Object} [options] - Options
   * @param {string} [options.label=''] - Label to display next to property input
   * @param {string} [options.detail=''] - Detail text to display in small font next to property input
   * @param {string} [options.placeholder=''] - Placeholder value to show in input when empty
   * @param {string} [options.type='string'] - Value type, one of: string/number/boolean or special type
   * @param {string|number|boolean} [options.value] - Initial input value (type should match)
   * @param {boolean} [options.request=false] - Whether to request this property during authentication
   * @param {boolean} [options.required=false] - Whether input value is required
   * @param {string} [options.mask] - Mask string to format the value using an input mask
   * @param {number} [options.min] - Minimum string length / number value
   * @param {number} [options.max] - Maximum string length / number value
   * @param {string} [options.pattern] - Regular expression that string should match
   * @param {string} [options.mode] - Input mode hint to the type of data that will be entered by the user
   * @param {string} [options.errorMessage] - Message to be displayed on field error
   * @param {string} [options.order=['month', 'day', 'year']] - The order in which a date field should be displayed
   * @param {string} [options.options] - Select field options
   */
  constructor({
    label = '',
    detail = '',
    placeholder = '',
    type = 'string',
    defaultValue,
    value,
    request = false,
    required = false,
    mask,
    min,
    max,
    pattern,
    mode,
    errorMessage,
    order,
    options,
   } = {}) {
    if (type === 'date' && !order) {
      order = ['month', 'day', 'year'];
    }

    Object.assign(this, {
      label,
      detail,
      placeholder,
      type,
      defaultValue,
      value,
      request,
      required,
      mask,
      min,
      max,
      pattern,
      mode,
      errorMessage,
      order,
      options,
    });

    // Disable property modification
    Object.freeze(this);
  }
}

export default IdentityProperty;