打字稿中的前置条件检查
Pre-condition checking in typescript
我有一个 typescript
class,其中有检查变量类型的方法。
doProcess()
开头如何确定使用哪种方法处理以下内容?
class MyClass {
public static arr : any[] = [];
// main method
public static doProcess(object : object , justForObjects : boolean = false){
if (justForObjects){
// here should specify which checking method use in *** line
} else {
}
for (let key in object){
if (object.hasOwnProperty(key)){
if (/* *** */){
MyClass.doProcess(object[key] , justObjects)
} else {
MyClass.arr.push(object[key])
}
}
}
return MyClass.arr;
}
// check variables type methods
private static is_object(value : any){
return !!(typeof value === 'object' && !(value instanceof Array) && value);
}
private static is_object_or_array(value : any){
return !!(typeof value === 'object' && value);
}
}
let object = {
'main color' : 'black',
'other colors' : {
'front color' : 'purple',
'back color' : 'yellow',
'inside colors' : {
'top color' : 'red',
'bottom color' : 'green'
}
}
}
MyClass.doProcess(object , true);
我知道它可以在同一个 for 循环中完成(如下所示),但我想先找到一种方法。
for (let key in object){
if(object.hasOwnProperty(key)){
if (justForObjects){
if (MyClass.is_object(object[key])){
// do something
}
} else {
if (MyClass.is_object_or_array(object[key])){
// do something else
}
}
}
}
感谢您的提示
看看递归方法是否对你有帮助:
interface CustomObject {
[path: string]: ObjectValue;
}
type ObjectValue = string | CustomObject | CustomObject[];
class MyClass {
/**
*
*
* @recursive
*/
public static doProcess(
object: CustomObject,
justForObjects: boolean = false
) {
for (const key in object) {
const value = object[key];
if (this.isObjectOrArray(value, justForObjects)) {
this.doProcess(value as CustomObject, justForObjects);
continue;
} else {
// do something;
continue;
}
}
}
/**
*
*
* Check is object or array
*/
private static isObjectOrArray(
value: ObjectValue,
justForObjects: boolean = false
) {
// objects and arrays
if (!justForObjects) return !!(typeof value === "object" && value);
if (Array.isArray(value)) throw new TypeError("Array value not allowed");
// only objects
return !!(typeof value === "object" && !(value instanceof Array) && value);
}
}
let object = {
"main color": "black",
"other colors": {
"front color": "purple",
"back color": "yellow",
"inside colors": {
"top color": "red",
"bottom color": "green"
}
}
} as CustomObject;
MyClass.doProcess(object, true);
函数可以简单地分配给变量,即:
public static doProcess(obj : object , justForObjects : boolean = false) {
var check_fn = justForObjects ? MyClass.is_object : MyClass.is_object_or_array;
for (let key in obj) {
if (check_fn(obj[key])) ...
}
}
// check variables type methods
private static is_object(value : any) : boolean { ... }
private static is_object_or_array(value : any) : boolean { ... }
我有一个 typescript
class,其中有检查变量类型的方法。
doProcess()
开头如何确定使用哪种方法处理以下内容?
class MyClass {
public static arr : any[] = [];
// main method
public static doProcess(object : object , justForObjects : boolean = false){
if (justForObjects){
// here should specify which checking method use in *** line
} else {
}
for (let key in object){
if (object.hasOwnProperty(key)){
if (/* *** */){
MyClass.doProcess(object[key] , justObjects)
} else {
MyClass.arr.push(object[key])
}
}
}
return MyClass.arr;
}
// check variables type methods
private static is_object(value : any){
return !!(typeof value === 'object' && !(value instanceof Array) && value);
}
private static is_object_or_array(value : any){
return !!(typeof value === 'object' && value);
}
}
let object = {
'main color' : 'black',
'other colors' : {
'front color' : 'purple',
'back color' : 'yellow',
'inside colors' : {
'top color' : 'red',
'bottom color' : 'green'
}
}
}
MyClass.doProcess(object , true);
我知道它可以在同一个 for 循环中完成(如下所示),但我想先找到一种方法。
for (let key in object){
if(object.hasOwnProperty(key)){
if (justForObjects){
if (MyClass.is_object(object[key])){
// do something
}
} else {
if (MyClass.is_object_or_array(object[key])){
// do something else
}
}
}
}
感谢您的提示
看看递归方法是否对你有帮助:
interface CustomObject {
[path: string]: ObjectValue;
}
type ObjectValue = string | CustomObject | CustomObject[];
class MyClass {
/**
*
*
* @recursive
*/
public static doProcess(
object: CustomObject,
justForObjects: boolean = false
) {
for (const key in object) {
const value = object[key];
if (this.isObjectOrArray(value, justForObjects)) {
this.doProcess(value as CustomObject, justForObjects);
continue;
} else {
// do something;
continue;
}
}
}
/**
*
*
* Check is object or array
*/
private static isObjectOrArray(
value: ObjectValue,
justForObjects: boolean = false
) {
// objects and arrays
if (!justForObjects) return !!(typeof value === "object" && value);
if (Array.isArray(value)) throw new TypeError("Array value not allowed");
// only objects
return !!(typeof value === "object" && !(value instanceof Array) && value);
}
}
let object = {
"main color": "black",
"other colors": {
"front color": "purple",
"back color": "yellow",
"inside colors": {
"top color": "red",
"bottom color": "green"
}
}
} as CustomObject;
MyClass.doProcess(object, true);
函数可以简单地分配给变量,即:
public static doProcess(obj : object , justForObjects : boolean = false) {
var check_fn = justForObjects ? MyClass.is_object : MyClass.is_object_or_array;
for (let key in obj) {
if (check_fn(obj[key])) ...
}
}
// check variables type methods
private static is_object(value : any) : boolean { ... }
private static is_object_or_array(value : any) : boolean { ... }