import {DateType, ChainableAttributeSpec, type} from "type-r"; const msDatePattern = /\/Date\(([0-9]+)\)\//; export class MicrosoftDateType extends DateType { convert( next ) { if( typeof next === 'string' ){ const msDate = msDatePattern.exec( next ); if( msDate ){ return new Date( Number( msDate[ 1 ] ) ); } } return DateType.prototype.convert.apply( this, arguments ); } toJSON( value ) { return value && `/Date(${ value.getTime() })/`; } } export const MicrosoftDate = new ChainableAttributeSpec({ type : Date, _metatype: MicrosoftDateType }) export const Timestamp = type( Date ).toJSON( x => x && x.getTime() );
return new ChainableAttributeSpec( { type : this, value : x, hasCustomDefault : true } ); }; Object.defineProperty( Function.prototype, 'isRequired', { get() { return this._isRequired || this.has.isRequired; }, set( x ){ this._isRequired = x; } }); Object.defineProperty( Function.prototype, 'asProp', { get() { return this.has.asProp; }, }); Object.defineProperty( Function.prototype, 'has', { get() { // workaround for sinon.js and other libraries overriding 'has' return this._has || type( this ); }, set( value ) { this._has = value; } } ); Object.defineProperties( Date, { microsoft: { value: MicrosoftDate }, timestamp: { value: Timestamp } } );
import { AttributeCheck, type } from 'type-r'; const urlPattern = /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/i; export function isUrl( x : string ) { return !x || urlPattern.test( x ); } (isUrl as AttributeCheck).error = 'Not valid URL' export const Url = type( String ).check( isUrl );
import { Model, type } from 'type-r'; export const SecondsInterval = Model.extend({ units: [ 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' ], defaults: { value: 1, interval: type( String ).value('months') }, properties: { seconds: function() { return this.getUnitValue(1) }, minutes: function() { return this.getUnitValue(60) }, hours: function() { return this.getUnitValue(3600) }, days: function() { return this.getUnitValue(86400) }, weeks: function() { return this.getUnitValue(604800) }, months: function() { return this.getUnitValue(2628000) }, years: function() { return this.getUnitValue(31536000) } }, getUnitValue( value ){ return value; },
import { AttributeCheck, type } from 'type-r'; const ipPattern = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; export function isIPAddress( x : string ) { return !x || ipPattern.test( x ); } (isIPAddress as AttributeCheck).error = 'Not valid IP address' export const IPAddress = type( String ).check( isIPAddress );
import { AttributeCheck, type } from 'type-r'; const emailPattern = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i; export function isEmail( x : string ) { return !x || !!x.match( emailPattern ); } (isEmail as AttributeCheck).error = 'Not valid email'; export const Email = type( String ).check( isEmail );