///|
/// Compare two member names by the UTF-16 code-unit ordering required by JCS.
pub fn compare_object_keys(left : String, right : String) -> Int {
  compare_keys(left, right)
}

///|
/// Expose the exact UTF-16 sort units used for an object member name.
pub fn utf16_sort_units(value : String) -> Array[Int] {
  utf16_units(value)
}

///|
pub fn sort_object_keys(keys : Array[String]) -> Array[String] {
  let output : Array[String] = []
  for key in keys {
    output.push(key)
  }
  let mut index = 1
  while index < output.length() {
    let current = output[index]
    let mut cursor = index
    while cursor > 0 && compare_keys(output[cursor - 1], current) > 0 {
      output[cursor] = output[cursor - 1]
      cursor = cursor - 1
    }
    output[cursor] = current
    index = index + 1
  }
  output
}

///|
pub fn keys_are_canonical(keys : Array[String]) -> Bool {
  let sorted = sort_object_keys(keys)
  if sorted.length() != keys.length() {
    return false
  }
  let mut index = 0
  while index < keys.length() {
    if keys[index] != sorted[index] {
      return false
    }
    index = index + 1
  }
  true
}