Embedding Test Example Documentation for API Usability and Code Flow | Task

Ole Ersoy
Feb - 16  -  1 min

Scenario

We want a simple example to illustrate our API, aid in coding the implementation, and unit test at the same time.

Approach


/**
 * @param source The source array
 * @param target The target array
 * @return An array containing any strings missing in the target array
 * 
 * @example
 * 
    let source = ['A', 'B']
    let target = ['A']
    let missing = findMissingStrings(source, target)
    expect(missing.length).toEqual(1)
    expect(missing[0]).toEqual('B')
 * 
 */
export function findMissingStrings(source: string[], target: string[]) {
    const missing = []
    source.forEach(s => {
        !target.includes(s) && missing.push(s)
    })
    return missing
}