@aptpod/data-viz-create-visual-parts-react
Version:
template of npm project with typescript
30 lines (27 loc) • 770 B
text/typescript
import bound from 'binary-search-bounds'
/**
* 指定要素以下 (less than or equal to) のIndexを検索する。
* 見つからない場合は-1を返す
*/
export const arraySearchIndexLe = <T>(
array: T[],
searchElement: T,
compiler: (sourceElement: T, searchElement_: T) => number,
): number => {
return bound.le(array, searchElement, compiler)
}
/**
* 配列の指定要素以上 (geater than or equal to) のIndexを取得する
* 見つからない場合は-1を返す
*/
export const arraySearchIndexGe = <T>(
array: T[],
searchElement: T,
compiler: (sourceElement: T, searchElement_: T) => number,
): number => {
const index = bound.ge(array, searchElement, compiler)
if (index >= array.length) {
return -1
}
return index
}