forked from FoundKeyGang/FoundKey
better name
This commit is contained in:
parent
21c50d42eb
commit
c128d2d1a3
1 changed files with 46 additions and 46 deletions
|
@ -8,20 +8,20 @@ import hasDuplicates from '../common/has-duplicates';
|
||||||
|
|
||||||
type Validator<T> = (value: T) => boolean | Error;
|
type Validator<T> = (value: T) => boolean | Error;
|
||||||
|
|
||||||
interface Factory {
|
interface Query {
|
||||||
/**
|
/**
|
||||||
* qedはQ.E.D.でもあり'QueryEnD'の略でもある
|
* qedはQ.E.D.でもあり'QueryEnD'の略でもある
|
||||||
*/
|
*/
|
||||||
qed: () => [any, Error];
|
qed: () => [any, Error];
|
||||||
|
|
||||||
required: () => Factory;
|
required: () => Query;
|
||||||
|
|
||||||
default: (value: any) => Factory;
|
default: (value: any) => Query;
|
||||||
|
|
||||||
validate: (validator: Validator<any>) => Factory;
|
validate: (validator: Validator<any>) => Query;
|
||||||
}
|
}
|
||||||
|
|
||||||
class FactoryCore implements Factory {
|
class QueryCore implements Query {
|
||||||
value: any;
|
value: any;
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ class FactoryCore implements Factory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BooleanFactory extends FactoryCore {
|
class BooleanQuery extends QueryCore {
|
||||||
value: boolean;
|
value: boolean;
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ class BooleanFactory extends FactoryCore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NumberFactory extends FactoryCore {
|
class NumberQuery extends QueryCore {
|
||||||
value: number;
|
value: number;
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ class NumberFactory extends FactoryCore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StringFactory extends FactoryCore {
|
class StringQuery extends QueryCore {
|
||||||
value: string;
|
value: string;
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -268,7 +268,7 @@ class StringFactory extends FactoryCore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ArrayFactory extends FactoryCore {
|
class ArrayQuery extends QueryCore {
|
||||||
value: any[];
|
value: any[];
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -338,7 +338,7 @@ class ArrayFactory extends FactoryCore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class IdFactory extends FactoryCore {
|
class IdQuery extends QueryCore {
|
||||||
value: mongo.ObjectID;
|
value: mongo.ObjectID;
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -384,7 +384,7 @@ class IdFactory extends FactoryCore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ObjectFactory extends FactoryCore {
|
class ObjectQuery extends QueryCore {
|
||||||
value: any;
|
value: any;
|
||||||
error: Error;
|
error: Error;
|
||||||
|
|
||||||
|
@ -434,24 +434,24 @@ type It = {
|
||||||
must: {
|
must: {
|
||||||
be: {
|
be: {
|
||||||
a: {
|
a: {
|
||||||
string: () => StringFactory;
|
string: () => StringQuery;
|
||||||
number: () => NumberFactory;
|
number: () => NumberQuery;
|
||||||
boolean: () => BooleanFactory;
|
boolean: () => BooleanQuery;
|
||||||
};
|
};
|
||||||
an: {
|
an: {
|
||||||
id: () => IdFactory;
|
id: () => IdQuery;
|
||||||
array: () => ArrayFactory;
|
array: () => ArrayQuery;
|
||||||
object: () => ObjectFactory;
|
object: () => ObjectQuery;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
expect: {
|
expect: {
|
||||||
string: () => StringFactory;
|
string: () => StringQuery;
|
||||||
number: () => NumberFactory;
|
number: () => NumberQuery;
|
||||||
boolean: () => BooleanFactory;
|
boolean: () => BooleanQuery;
|
||||||
id: () => IdFactory;
|
id: () => IdQuery;
|
||||||
array: () => ArrayFactory;
|
array: () => ArrayQuery;
|
||||||
object: () => ObjectFactory;
|
object: () => ObjectQuery;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -459,24 +459,24 @@ const it = (value: any) => ({
|
||||||
must: {
|
must: {
|
||||||
be: {
|
be: {
|
||||||
a: {
|
a: {
|
||||||
string: () => new StringFactory(value),
|
string: () => new StringQuery(value),
|
||||||
number: () => new NumberFactory(value),
|
number: () => new NumberQuery(value),
|
||||||
boolean: () => new BooleanFactory(value)
|
boolean: () => new BooleanQuery(value)
|
||||||
},
|
},
|
||||||
an: {
|
an: {
|
||||||
id: () => new IdFactory(value),
|
id: () => new IdQuery(value),
|
||||||
array: () => new ArrayFactory(value),
|
array: () => new ArrayQuery(value),
|
||||||
object: () => new ObjectFactory(value)
|
object: () => new ObjectQuery(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
expect: {
|
expect: {
|
||||||
string: () => new StringFactory(value),
|
string: () => new StringQuery(value),
|
||||||
number: () => new NumberFactory(value),
|
number: () => new NumberQuery(value),
|
||||||
boolean: () => new BooleanFactory(value),
|
boolean: () => new BooleanQuery(value),
|
||||||
id: () => new IdFactory(value),
|
id: () => new IdQuery(value),
|
||||||
array: () => new ArrayFactory(value),
|
array: () => new ArrayQuery(value),
|
||||||
object: () => new ObjectFactory(value)
|
object: () => new ObjectQuery(value)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -493,26 +493,26 @@ function x(value: any, type: 'object', isRequired?: boolean, validator?: Validat
|
||||||
function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator<any> | Validator<any>[]): any {
|
function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator<any> | Validator<any>[]): any {
|
||||||
if (typeof type === 'undefined') return it(value);
|
if (typeof type === 'undefined') return it(value);
|
||||||
|
|
||||||
let factory: Factory = null;
|
let q: Query = null;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'id': factory = it(value).expect.id(); break;
|
case 'id': q = it(value).expect.id(); break;
|
||||||
case 'string': factory = it(value).expect.string(); break;
|
case 'string': q = it(value).expect.string(); break;
|
||||||
case 'number': factory = it(value).expect.number(); break;
|
case 'number': q = it(value).expect.number(); break;
|
||||||
case 'boolean': factory = it(value).expect.boolean(); break;
|
case 'boolean': q = it(value).expect.boolean(); break;
|
||||||
case 'array': factory = it(value).expect.array(); break;
|
case 'array': q = it(value).expect.array(); break;
|
||||||
case 'set': factory = it(value).expect.array().unique(); break;
|
case 'set': q = it(value).expect.array().unique(); break;
|
||||||
case 'object': factory = it(value).expect.object(); break;
|
case 'object': q = it(value).expect.object(); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isRequired) factory = factory.required();
|
if (isRequired) q = q.required();
|
||||||
|
|
||||||
if (validator) {
|
if (validator) {
|
||||||
(Array.isArray(validator) ? validator : [validator])
|
(Array.isArray(validator) ? validator : [validator])
|
||||||
.forEach(v => factory = factory.validate(v));
|
.forEach(v => q = q.validate(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
return factory;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default x;
|
export default x;
|
||||||
|
|
Loading…
Reference in a new issue