In development, it is often necessary to assemble data and submit it to the API, or filter and display the response data from the API. At this time, the ofliterjs library can quickly solve the data processing problem.

OfilterJs is a data object {} filter processor for Javascript, providing simpler, more convenient and efficient data processing operations for development.

ofilter-js

Related Links

Supported languages

Function

  • 🍑 filterValue filter data
  • 🍐 getValue read data
  • 🍎 resetValue reset data

Install the module

$ npm i ofilterjs

or other pnpm, cnpm, yarn…

$ pnpm i ofilterjs

import module

 import ofjs from 'ofilterjs'
 
 // const ofjs = require('ofilterjs')

1. Data filtering

filterValue([数据对象], [配置项]…[扩展数据])

1.1 Filtering/reorganizing data

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version_number: 10001
        }
    }
}

const newData = ofjs.filterValue(data, {
    name: 'lib.pkg.name',
    versionNumber: 'lib.pkg.version_number',
})
console.log(newData)

/** 结果
   newData = {
        name: 'ofilterjs',
        versionNumber: 10001
   } 
*/

1.2 Specifying the value directly

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version_number: 10001
        }
    }
}

const newData = ofjs.filterValue(data, {
    name: 'lib.pkg.name',
    type: {
        value: 'type value'
    }
})
console.log(newData)

/** 结果
   newData = {
        name: 'ofilterjs',
        type: 'type value'
   } 
*/

1.3 Set default values

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version_number: 10001
        }
    }
}

const newData = ofjs.filterValue(data, {
    name: 'lib.pkg.name',
    alias: {
        key: 'lib.pkg.alias',
        default: 'Default alias'
    },
    type: {
        key: 'lib.pkg.type',
        default: 'Npm pkg'
    }
})
console.log(newData)

/** 结果
   newData = {
        name: 'ofilterjs',
        alias: 'Default alias',
        type: 'Npm pkg'
   } 
*/

1.4 Custom filter callback

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version_number: 10001
        }
    }
}

const newData = ofjs.filterValue(data, {
    name: 'lib.pkg.name',
    alias: {
        key: 'lib.pkg.alias',
        filter: (value, source) => {
            if (value !== '') return value
            return 'This is ' + (source?.lib?.pkg?.name || 'unknown')
        }
    },
    type: {
        key: 'lib.pkg.type',
        filter: (value, source) => {
            return 'Filter npm'
        }
    }
})
console.log(newData)

/** 结果
   newData = {
        name: 'ofilterjs',
        alias: 'This is ofilterjs',
        type: 'Filter npm'
   } 
*/

1.5 Merge into result set

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version_number: 10001
        }
    }
}

const newData = ofjs.filterValue(data, {
    name: 'lib.pkg.name',
    _: {
        merge: true,
        filter: (_, source) => {
            if (source?.lib?.pkg?.name === 'ofilterjs') {
                return {
                   support: ['js', 'ts', 'es']
                }
            }
            return {}
        }
    },
    _1: {
        merge: true,
        filter: (_, source) => {
            return { more: 'more data ...' }
        }
    },
  }
})
console.log(newData)

/** 结果
   newData = {
        name: 'ofilterjs',
        support: ['js', 'ts', 'es'],
        more: 'more data ...'
   } 
*/

1.6 Merging Extended Data

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version_number: 10001
        }
    }
}

const newData = ofjs.filterValue(data, {
    name: 'lib.pkg.name'
  }
}, {
    name1: 'ofilter'
}, {
    name2: 'object filter'
})
console.log(newData)

/** 结果
   newData = {
        name: 'ofilterjs',
        name1: 'ofilter',
        name2: 'object filter'
   } 
*/

2. Data reading

getValue([名称访问字符串], [默认值])

2.1 Value read/depth read

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            version: 10001
        },
        support: ['js', 'ts', 'es']
    }
}

// 原始方式
const name = data && data['lib'] && data['lib']['name'] && data['lib']['pkg']['name'] || 'unknown'
console.log(name)   // ofilterjs

// es6的 ?. 方式
const name = data?.lib?.pkg?.name || 'unknown'
console.log(name)   // ofilterjs

// 使用 ofilterjs 方式
const name = ofjs.getValue(data, 'lib.pkg.name', 'unknown')
console.log(name)   // ofilterjs

2.2 Priority read value

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es']
    }
}

// 原始方式
const currnet = data && data['lib'] && data['lib']['pkg'] || {}
const alias = currnet['alias'] || currnet['name'] || 'unknown'
console.log(alias)   // ofilterjs

// es6的 ?. 方式
const alias = data?.lib?.pkg?.alias || data?.lib?.pkg?.name || 'unknown'
console.log(alias)   // ofilterjs

// 使用 ofilterjs 方式
const alias = ofjs.getValue(data, 'lib.pkg.alias|lib.pkg.name', 'unknown')
console.log(name)   // ofilterjs

2.3 Array index subscript reading

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es']
    }
}

// 原始方式
const support = data && data['lib'] && data['lib']['support'] || {}
const su = support[0] || 'unknown'
console.log(su)   // js

// es6的 ?. 方式
const su = data?.lib?.support?.[0] || 'unknown'
console.log(su)   // js

// 使用 ofilterjs 方式
const su = ofjs.getValue(data, 'lib.support.0', 'unknown')
console.log(su)   // js

3. Data reset

resetValue([数据对象], [配置,可选])

Tip: By default property names prefixed with ‘_’ will not participate in automatic reset, but can be specified using manual configuration.

3.1 Automatically recognize value type reset value

Shallow reset (valid for the first layer)

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es'],
        _private: 'private attr' 
    },
    lib2: {
        pkg: {}
    }
}

ofjs.resetValue(data, false)

/**  结果
const data = {
    lib: {},
    lib2: {}
}
*/

Deep reset (default valid for all layers)

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es'],
        _private: 'private attr' 
    },
    lib2 : {
        pkg: {
            name: 'ofilter'
        }
    }
}

ofjs.resetValue(data, true)

/**  结果
const data = {
    lib: {
        pkg: {
            name: '',
            alias: '',
            version: 0
        },
        support: [],
        _private: 'private attr' 
    },
    lib2 : {
        pkg: {
            name: ''
        }
    }
}
*/

deep reset – specifies the number of depth layers, does not specify the starting position (default starts from 0)

const data = {
    // 0层
    name: 'lib_list',
    lib: {
        // 1层
        type: 'npm',
        pkg: {
            // 2层
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: {
            'js' : 'javascript',
            'ts' : 'typescript'
        },
        _private: 'private attr' 
    },
    lib2 : {
        type: 'npm',
        pkg: {
            name: 'ofilter'
        }
    }
}

// 2代表深度为2个层数,表示:0 ~ (0+2),不包含(0+2)
ofjs.resetValue(data, true, 2)

/**  结果
const data = {
    // 0层
    name: '',   // 被重置
    lib: {
        // 1层
        type: '',   // 被重置
        pkg: {
            // 2层
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: {
            'js' : 'javascript',
            'ts' : 'typescript'
        },
        _private: 'private attr' 
    },
    lib2 : {
        type: '',   // 被重置
        pkg: {
            name: 'ofilter'
        }
    }
}
*/

deep reset – specifies the number of depth levels, and also specifies the starting position

const data = {
    // 0层
    name: 'lib_list',
    lib: {
        // 1层
        type: 'npm',
        pkg: {
            // 2层
            name: 'ofilterjs',
            alias: '',
            version: 10001,
            support: {
                 // 3层
                'js' : 'javascript',
                'ts' : 'typescript'
            }
        },
        _private: 'private attr' 
    },
    lib2 : {
        type: 'npm',
        pkg: {
            name: 'ofilter'
        }
    }
}

// 2代表深度为2个层数,表示:1 ~ (1+2),不包含(1+2)
ofjs.resetValue(data, true, 2, 1)

/**  结果
const data = {
    // 0层
    name: 'lib_list',
    lib: {
        // 1层
        type: '',   // 被重置
        pkg: {
            // 2层
            name: '',   // 被重置
            alias: '',  // 被重置
            version: 0, // 被重置
            support: {
                 // 3层
                'js' : 'javascript',
                'ts' : 'typescript'
            }
        },
        _private: 'private attr' 
    },
    lib2 : {
        type: '',   // 被重置
        pkg: {
            name: ''    // 被重置
        }
    }
}
*/

3.2 Manually specify the reset field

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es'],
        _private: 'private attr' 
    }
}

ofjs.resetValue(data, [
    'lib.pkg.name',
    'lib.pkg.version',
    'lib.pkg._private'
])

/**  结果
const data = {
    lib: {
        pkg: {
            name: '',
            alias: '',
            version: 0
        },
        support: ['js', 'ts', 'es'],
        _private: '' 
    }
}
*/

3.3 Manually configure the field to set the specified value

const data = {
    lib: {
        pkg: {
            name: 'ofilterjs',
            alias: '',
            version: 10001
        },
        support: ['js', 'ts', 'es']
    }
}

ofjs.resetValue(data, {
    'lib.pkg.name': 'newname',
    'lib.pkg.version': 10002
})

/** 结果
const data = {
    lib: {
        pkg: {
            name: 'newname',
            alias: '',
            version: 10002
        },
        support: ['js', 'ts', 'es']
    }
}
*/

#ofliterjs #Homepage #Documentation #Downloads #Javascript #data #object #filtering #processing #library #News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *