Web.Components: utils: splitted email utility into two files

This commit is contained in:
Daniil Senkiv 2019-10-15 15:26:31 +03:00
parent 689593f9e3
commit 3c2fb281b9
5 changed files with 156 additions and 154 deletions

View File

@ -1,7 +1,7 @@
import React from 'react';
import { mount, shallow } from 'enzyme';
import EmailInput from '.';
import { EmailSettings } from '../../utils/email';
import { EmailSettings } from '../../utils/email/';
const baseProps = {

View File

@ -3,7 +3,7 @@ import styled from 'styled-components'
import PropTypes from 'prop-types'
import isEqual from "lodash/isEqual";
import TextInput from '../text-input'
import { EmailSettings, parseAddress, checkAndConvertEmailSettings, isEqualEmailSettings } from '../../utils/email';
import { EmailSettings, parseAddress, checkAndConvertEmailSettings, isEqualEmailSettings } from '../../utils/email/';
const borderColor = {
default: '#D0D5DA',

View File

@ -1,6 +1,7 @@
import emailAddresses from "email-addresses";
import punycode from "punycode";
import { parseErrorTypes } from "./constants";
import { parseErrorTypes } from "./../constants";
import { EmailSettings } from './index';
const getParts = string => {
let mass = [];
@ -260,154 +261,3 @@ export class Email {
return false;
};
}
export class EmailSettings {
constructor() {
this.allowDomainPunycode = false;
this.allowLocalPartPunycode = false;
this.allowDomainIp = false;
this.allowStrictLocalPart = true;
this.allowSpaces = false;
this.allowName = false;
this.allowLocalDomainName = false;
}
get allowDomainPunycode() {
return this._allowDomainPunycode;
}
set allowDomainPunycode(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowDomainPunycode = value;
}
else {
throw `Invalid value ${value} for allowDomainPunycode option. Use boolean value`
}
}
get allowLocalPartPunycode() {
return this._allowLocalPartPunycode;
}
set allowLocalPartPunycode(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowLocalPartPunycode = value;
}
else {
throw `Invalid value ${value} for allowLocalPartPunycode option. Use boolean value`
}
}
get allowDomainIp() {
return this._allowDomainIp;
}
set allowDomainIp(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowDomainIp = value;
}
else {
throw `Invalid value ${value} for allowDomainIp option. Use boolean value`
}
}
get allowStrictLocalPart() {
return this._allowStrictLocalPart;
}
set allowStrictLocalPart(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowStrictLocalPart = value;
}
else {
throw `Invalid value ${value} for allowStrictLocalPart option. Use boolean value`
}
}
get allowSpaces() {
return this._allowSpaces;
}
set allowSpaces(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowSpaces = value;
}
else {
throw `Invalid value ${value} for allowSpaces option. Use boolean value`
}
}
get allowName() {
return this._allowName;
}
set allowName(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowName = value;
}
else {
throw `Invalid value ${value} for allowName option. Use boolean value`
}
}
get allowLocalDomainName() {
return this._allowLocalDomainName;
}
set allowLocalDomainName(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowLocalDomainName = value;
}
else {
throw `Invalid value ${value} for allowLocalDomainName option. Use boolean value`
}
}
getSettings() {
return {
allowDomainPunycode: this.allowDomainPunycode,
allowLocalPartPunycode: this.allowLocalPartPunycode,
allowDomainIp: this.allowDomainIp,
allowStrictLocalPart: this.allowStrictLocalPart,
allowSpaces: this.allowSpaces,
allowName: this.allowName,
allowLocalDomainName: this.allowLocalDomainName
}
}
}
export const checkAndConvertEmailSettings = (settings) => {
if (typeof settings === 'object' && !(settings instanceof EmailSettings)) {
const defaultSettings = new EmailSettings();
Object.keys(settings).map((item) => {
if (defaultSettings[item] !== null && defaultSettings[item] != settings[item]) {
defaultSettings[item] = settings[item];
}
});
return defaultSettings;
}
else if (typeof settings === 'object' && settings instanceof EmailSettings) {
return settings;
}
}
export const isEqualEmailSettings = (settings1, settings2) => {
const comparedProperties = [
'allowDomainPunycode',
'allowLocalPartPunycode',
'allowDomainIp',
'allowStrictLocalPart',
'allowSpaces',
'allowName',
'allowLocalDomainName'
];
const propLength = comparedProperties.length;
for (let i = 0; i < propLength; i++) {
const comparedProp = comparedProperties[i]
if (settings1[comparedProp] !== settings2[comparedProp]) {
return false;
}
}
return true;
}

View File

@ -0,0 +1,150 @@
export class EmailSettings {
constructor() {
this.allowDomainPunycode = false;
this.allowLocalPartPunycode = false;
this.allowDomainIp = false;
this.allowStrictLocalPart = true;
this.allowSpaces = false;
this.allowName = false;
this.allowLocalDomainName = false;
}
get allowDomainPunycode() {
return this._allowDomainPunycode;
}
set allowDomainPunycode(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowDomainPunycode = value;
}
else {
throw `Invalid value ${value} for allowDomainPunycode option. Use boolean value`
}
}
get allowLocalPartPunycode() {
return this._allowLocalPartPunycode;
}
set allowLocalPartPunycode(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowLocalPartPunycode = value;
}
else {
throw `Invalid value ${value} for allowLocalPartPunycode option. Use boolean value`
}
}
get allowDomainIp() {
return this._allowDomainIp;
}
set allowDomainIp(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowDomainIp = value;
}
else {
throw `Invalid value ${value} for allowDomainIp option. Use boolean value`
}
}
get allowStrictLocalPart() {
return this._allowStrictLocalPart;
}
set allowStrictLocalPart(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowStrictLocalPart = value;
}
else {
throw `Invalid value ${value} for allowStrictLocalPart option. Use boolean value`
}
}
get allowSpaces() {
return this._allowSpaces;
}
set allowSpaces(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowSpaces = value;
}
else {
throw `Invalid value ${value} for allowSpaces option. Use boolean value`
}
}
get allowName() {
return this._allowName;
}
set allowName(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowName = value;
}
else {
throw `Invalid value ${value} for allowName option. Use boolean value`
}
}
get allowLocalDomainName() {
return this._allowLocalDomainName;
}
set allowLocalDomainName(value) {
if (value !== undefined && typeof value === 'boolean') {
this._allowLocalDomainName = value;
}
else {
throw `Invalid value ${value} for allowLocalDomainName option. Use boolean value`
}
}
getSettings() {
return {
allowDomainPunycode: this.allowDomainPunycode,
allowLocalPartPunycode: this.allowLocalPartPunycode,
allowDomainIp: this.allowDomainIp,
allowStrictLocalPart: this.allowStrictLocalPart,
allowSpaces: this.allowSpaces,
allowName: this.allowName,
allowLocalDomainName: this.allowLocalDomainName
}
}
}
export const checkAndConvertEmailSettings = (settings) => {
if (typeof settings === 'object' && !(settings instanceof EmailSettings)) {
const defaultSettings = new EmailSettings();
Object.keys(settings).map((item) => {
if (defaultSettings[item] !== null && defaultSettings[item] != settings[item]) {
defaultSettings[item] = settings[item];
}
});
return defaultSettings;
}
else if (typeof settings === 'object' && settings instanceof EmailSettings) {
return settings;
}
}
export const isEqualEmailSettings = (settings1, settings2) => {
const comparedProperties = [
'allowDomainPunycode',
'allowLocalPartPunycode',
'allowDomainIp',
'allowStrictLocalPart',
'allowSpaces',
'allowName',
'allowLocalDomainName'
];
const propLength = comparedProperties.length;
for (let i = 0; i < propLength; i++) {
const comparedProp = comparedProperties[i]
if (settings1[comparedProp] !== settings2[comparedProp]) {
return false;
}
}
return true;
}

View File

@ -0,0 +1,2 @@
export { parseAddress } from './email';
export { EmailSettings, checkAndConvertEmailSettings, isEqualEmailSettings } from './emailSettings';