"use strict";
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.replace.js");
require("core-js/modules/es.string.trim.js");
require("core-js/modules/es.string.split.js");
require("core-js/modules/es.array.includes.js");
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.array.join.js");
require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.last-index-of.js");
require("core-js/modules/es.array.splice.js");
var React = _interopRequireWildcard(require("react"));
var _reactBootstrap = require("react-bootstrap");
var _reactFontawesome = require("@fortawesome/react-fontawesome");
var _validationLabel = _interopRequireDefault(require("../common/validation-label"));
var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");
var _excluded = ["empty", "error", "onChange", "storedNameValue"];
/*
* Copyright (C) 2016 Ben Ockmore
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* Removes all period characters (dots) from the input string, returning a new
* string.
*
* @param {String} name the input string to strip
* @returns {String} the string with dots removed
*/
function stripDot(name) {
return name.replace(/\./g, '');
}
function makeSortName(name) {
var articles = ['a', 'an', 'the', 'los', 'las', 'el', 'la'];
var suffixes = ['i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix', 'x', 'xi', 'xii', 'xiii', 'xiv', 'xv', 'jr', 'junior', 'sr', 'senior', 'phd', 'md', 'dmd', 'dds', 'esq'];
/*
* Remove leading and trailing spaces, and return a blank sort name if
* the string is empty
*/
var trimmedName = name.trim();
if (trimmedName.length === 0) {
return '';
}
var words = trimmedName.replace(/,/g, '').split(' ');
// If there's only one word, simply copy the name as the sort name
if (words.length === 1) {
return trimmedName;
}
// First, check if sort name is for collective, by detecting article
var firstWord = stripDot(words[0]);
var firstWordIsArticle = articles.includes(firstWord.toLowerCase());
if (firstWordIsArticle) {
// The Collection of Stories --> Collection of Stories, The
return "".concat(words.slice(1).join(' '), ", ").concat(firstWord);
}
/*
* From here on, it is assumed that the sort name is for a person
* Split suffixes
*/
var isWordSuffix = words.map(function (word) {
return suffixes.includes(stripDot(word).toLowerCase());
});
var lastSuffix = isWordSuffix.lastIndexOf(false) + 1;
// Test this to check that splice will not have a 0 deleteCount
var suffixWords = lastSuffix < words.length ? words.splice(lastSuffix) : [];
// Rearrange names to (last name, other names)
var INDEX_BEFORE_END = -1;
var _words$splice = words.splice(INDEX_BEFORE_END),
_words$splice2 = (0, _slicedToArray2.default)(_words$splice, 1),
lastName = _words$splice2[0];
if (suffixWords.length > 0) {
lastName += " ".concat(suffixWords.join(' '));
}
return "".concat(lastName, ", ").concat(words.join(' '));
}
/**
* Presentational component. This component renders a plain text input which
* incorporates a 'Guess Sort Name' button, and a ValidationLabel for a field
* labelled 'Sort Name'. When clicked, the 'Guess Sort Name' button uses the
* name value passed to the component to guess an appropriate sort name.
*
* @param {Object} props - The properties passed to the component.
* @param {boolean} props.error - Passed to the ValidationLabel within the
* component to indicate a validation error.
* @param {boolean} props.empty - Passed to the ValidationLabel within the
* component to indicate that the field is empty.
* @param {Function} props.onChange - Function to be called when the value in
* the wrapped input changes.
* @param {string} props.storedNameValue - The name value to be used to
* generate the sort name when the 'Guess Sort Name' button is clicked.
* @returns {Object} a React component containing the rendered input
*/
function SortNameField(_ref) {
var empty = _ref.empty,
error = _ref.error,
onChange = _ref.onChange,
storedNameValue = _ref.storedNameValue,
rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
var input;
function handleGuessClick() {
var generatedSortName = makeSortName(storedNameValue);
if (input) {
input.value = generatedSortName;
}
if (onChange) {
onChange({
target: {
value: generatedSortName
}
});
}
}
function handleCopyClick() {
if (input) {
input.value = storedNameValue;
}
if (onChange) {
onChange({
target: {
value: storedNameValue
}
});
}
}
var label = /*#__PURE__*/React.createElement(_validationLabel.default, {
empty: empty,
error: error
}, "Sort Name");
/* eslint-disable react/jsx-no-bind */
var guessButton = /*#__PURE__*/React.createElement(_reactBootstrap.Button, {
variant: "primary",
onClick: handleGuessClick
}, "Guess");
var copyButton = /*#__PURE__*/React.createElement(_reactBootstrap.Button, {
className: "ml-1",
variant: "primary",
onClick: handleCopyClick
}, "Copy");
/* eslint-enable react/jsx-no-bind */
var tooltip = /*#__PURE__*/React.createElement(_reactBootstrap.Tooltip, null, "Alphabetical sorting name. Examples: 'Dickens, Charles', 'Christmas Carol, A'.", /*#__PURE__*/React.createElement("br", null), "You can try to fill it automatically with the guess button");
return /*#__PURE__*/React.createElement(_reactBootstrap.Form.Group, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Label, null, label, /*#__PURE__*/React.createElement(_reactBootstrap.OverlayTrigger, {
delay: 50,
overlay: tooltip
}, /*#__PURE__*/React.createElement(_reactFontawesome.FontAwesomeIcon, {
className: "margin-left-0-5",
icon: _freeSolidSvgIcons.faQuestionCircle
}))), /*#__PURE__*/React.createElement(_reactBootstrap.InputGroup, null, /*#__PURE__*/React.createElement(_reactBootstrap.Form.Control, (0, _extends2.default)({
/* eslint-disable-next-line react/jsx-no-bind */
ref: function ref(node) {
input = node;
},
type: "text",
onChange: onChange
}, rest)), /*#__PURE__*/React.createElement(_reactBootstrap.InputGroup.Append, null, guessButton, copyButton)));
}
SortNameField.displayName = 'SortNameField';
SortNameField.defaultProps = {
empty: false,
error: false,
onChange: null
};
var _default = SortNameField;
exports.default = _default;
//# sourceMappingURL=sort-name-field.js.map