New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

code-point-mapping

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-point-mapping

Map between javascript string indices and unicode code point offsets effectively

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-85.71%
Maintainers
1
Weekly downloads
 
Created
Source

code-point-mapping provides a way to map between utf16 string indices and unicode code point offsets effectively.

Unicode code points require either one or two utf16 code units to represent them. Characters outside the Basic Multilingual Plane are represented as two surrogate pairs. This means as soon as you use characters (like Emoji) that are in this state, you need to do some work to map between utf16 indexes and unicode code point offsets.

This package was designed for use with automerge, which requires that you specify offsets in terms of unicode code points, and so only the APIs I needed to make that work are here.

For example:

import CodePointMapping from 'code-point-mapping'
import * as automerge from '@automerge/automerge'

let doc1 = automerge.from({ str: new automerge.Text('😀🎉✈️') })
let cpm = new CodePointMapping(doc1.str)

cpm.indexForCodepoint(1) // => 2

doc1 = automerge.change(doc1, d => {
  d.str.deleteAt(...cpm.deleteAt(0, 2)) // d.str.deleteAt(0, 1)
  d.str.insertAt(...cpm.insertAt(2, '🧟‍♀️')) // d.str.insertAt(1, ..."🧟‍♀️")
})

NOTE: This library assumes that your strings are valid unicode and do not contain unpaired surrogates.

FAQs

Package last updated on 02 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc