本文整理匯總了TypeScript中type-r.type函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript type函數的具體用法?TypeScript type怎麽用?TypeScript type使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了type函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: convert
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() );
示例2: ChainableAttributeSpec
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
}
} );
示例3: isUrl
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 );
示例4: type
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;
},
示例5: isIPAddress
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 );
示例6: isEmail
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 );