Skip to content

Instantly share code, notes, and snippets.

@ci7lus

ci7lus/parser.ts

Last active Dec 3, 2019
Embed
What would you like to do?
Tweet Entity Parser written in TypeScript / https://is.gd/ltCGV5 (scrapbox.io/ci7lus)
// MIT License - Copyright (c) 2019 ci7lus
const text = Array.from(status.full_text)
const entities = Object.entries(status.entities)
.filter(entity => entity[1])
.flatMap(([entityType, entityArr]) => {
return entityArr.map((entity: any) => {return {entityType, ...entity}})
}) as ({ entityType: string; indices: [number, number], [name: string]: unknown })[]
const slices = Object.fromEntries(entities.map(entity => [entity.indices[0], entity]))
const parts = []
let current = 0
for (let i = 0; i < text.length; i++) {
if (i in slices) {
const slice = slices[i]
parts.push(text.slice(current, i).join(""))
parts.push(text.slice(i, slice.indices[1]).join(""))
current = slice.indices[1]
}
}
parts.push(text.slice(current).join(""))
console.log(parts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.