Skip to content

Instantly share code, notes, and snippets.

@qwasfun
Created January 9, 2025 16:49
Show Gist options
  • Select an option

  • Save qwasfun/ec06711635eca53cdb36db5475611213 to your computer and use it in GitHub Desktop.

Select an option

Save qwasfun/ec06711635eca53cdb36db5475611213 to your computer and use it in GitHub Desktop.
字典
export function defaultToString(item) {
if (item === null) {
return 'NULL'
}
if (item === undefined) {
return undefined
}
if (typeof item === 'string' || item instanceof String) {
return `${item}`
}
return item.toString()
}
export class ValuePair {
constructor(key, value) {
this.key = key
this.value = value
}
toString() {
return `[#${this.key}:${this.value}]`
}
}
export class Dictionary {
constructor(toStrFn = defaultToString) {
this.toStrFn = toStrFn
this.table = {}
}
// 检测一个键是否存在于字典中
hasKey(key) {
return this.table[this.toStrFn(key)] != null
}
set(key, value) {
if (key != null && value != null) {
const tableKey = this.toStrFn(key)
this.table[tableKey] = new ValuePair(key, value)
return true
}
return false
}
remove(key) {
if (this.hasKey(key)) {
delete this.table[this.toStrFn(key)]
return true
}
return false
}
get(key) {
const valuePair = this.table[this.toStrFn(key)]
return valuePair == null ? undefined : valuePair.value
}
keyValues() {
return Object.values(this.table)
}
//
keys() {
return this.keyValues().map(valuePair => valuePair.key)
}
values() {
return this.keyValues().map(valuePair => valuePair.value)
}
// 迭代字典中的每个键值对
forEach(callbackFn) {
const valuePairs = this.keyValues()
for (let i = 0; i < valuePairs.length; i++) {
const result = callbackFn(valuePairs[i].key, valuePairs[i].value)
if (result === false) {
break
}
}
}
size() {
return Object.keys(this.table).length
}
isEmpty() {
return this.size() === 0
}
clear() {
this.table = []
}
toString() {
if (this.isEmpty()) {
return ''
}
const valuePairs = this.keyValues()
let objString = `${valuePairs[0].toString()}`
for (let i = 1; i < valuePairs.length; i++) {
objString = `${objString}${valuePairs[i].toString()}`
}
return objString
}
}
// const dictionary = new Dictionary()
// dictionary.set('Alice', 'alice@example.com')
// dictionary.set('Bob', 'bob@example.com')
// dictionary.set('Chris', 'chris@example.com')
// console.log(dictionary.hasKey('Alice')) //true
// console.log(dictionary.size()) //3
// console.log(dictionary.keys()) // [ 'Alice', 'Bob', 'Chris' ]
// console.log(dictionary.values())
// /*[ 'alice@example.com', 'bob@example.com', 'chris@example.com' ]*/
// console.log(dictionary.keyValues())
// /*
// [
// ValuePair { key: 'Alice', value: 'alice@example.com' },
// ValuePair { key: 'Bob', value: 'bob@example.com' },
// ValuePair { key: 'Chris', value: 'chris@example.com' }
// ]
// */
// console.log(dictionary.get('Chris')) // chris@example.com
// console.log(dictionary.remove('Bob')) //true
// dictionary.forEach((k, v) => {
// console.log('forEach:', `key:${k},value:${v}`)
// })
// /*
// forEach: key:Alice,value:alice@example.com
// forEach: key:Chris,value:chris@example.com
// */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment