Search code examples
palantir-foundryfoundry-code-repositories

TypeScript: return only first value of an ObjectSet


I am pretty new to TypeScript and especially to the custom Palantir implementation for Object(Sets). What I am trying to archive: I'd like to filter an ObjectSet down to some specific values. Then I'd like to return the first of these values. In fact I'd just like to return a single line. What I've done so far:

    @Function()
    public nextUnprocessedValueString(inputObject: ObjectSet<CombinedSentencesForTagging>): ObjectSet<CombinedSentencesForTagging>{
        const result = Objects.search().combinedSentencesForTagging().filter(f => f.customerFeedback.exactMatch('i like it very much.'))
        return result

The result looks like this: result

What I would need is only the first line (or a random line).

Thanks!


Solution

  • Give this a try:

    @Function()
    public nextUnprocessedValueString(inputObject: ObjectSet<CombinedSentencesForTagging>): CombinedSentencesForTagging {
        const result = 
               inputObject.filter(f => f.customerFeedback.exactMatch('i like it very much.'))
                          .orderBy(f => f.customerFeedback.asc())
                          .take(1);
        
        return result[0];
    }
    

    Here are the changes I made to the original function:

    1. Changed the return type to a single CombinedSentencesForTagging object.
    2. Modified the filter line to run on the inputObject specified in the function's parameters
    3. Used the orderBy and take filter clauses to pick just one of the filtered results.