Created
April 25, 2022 07:16
-
-
Save shaomingquan/c9bbf058c0f7760995393acdd4ef08c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
// "internationalization" | |
function abbr (input) { | |
const length = input.length | |
if (length < 3) { | |
return input | |
} | |
return input[0] + (length - 2) + input[length - 1] | |
} | |
const a = 'a'.charCodeAt(0) | |
const z = a + 25 | |
function isLowerChar (char) { | |
const code = char.charCodeAt(0) | |
return code >= a && code <= z | |
} | |
function abbrPlus (input) { | |
const l = input.length | |
let ret = '' | |
let index = 0 | |
let tmp = '' | |
while (index < l) { | |
const currentChar = input[index] | |
const nextChar = input[index + 1] | |
if (isLowerChar(currentChar)) { | |
tmp += currentChar | |
if (!nextChar || !isLowerChar(nextChar)) { | |
ret += abbr(tmp) | |
tmp = '' | |
} | |
} else { | |
tmp += currentChar | |
if (!nextChar || isLowerChar(nextChar)) { | |
ret += tmp | |
tmp = '' | |
} | |
} | |
index ++ | |
} | |
return ret | |
} | |
var input = "%%%jack-jadddne_@@22tom$$" | |
// 2 | |
// [0, 1, 2, 3, 4, 5, 6, 7] | |
function bs (arr, target) { | |
const l = arr.length | |
let left = 0 | |
let right = l - 1 | |
while (left <= right) { | |
const mid = left + Math.floor((right - left) / 2) | |
const midVal = arr[mid] | |
if (midVal === target) { | |
return mid | |
} else if (midVal < target) { | |
left = mid + 1 | |
} else { | |
right = mid - 1 | |
} | |
} | |
return left | |
} | |
var arr = [-5, 0, 1, 2, 3, 4, 5, 6, 7] | |
// 3 | |
async function fetch (url) { | |
console.log(`fetching... ${url}`); | |
return `content of ${url}` | |
} | |
// | |
function makeCachedFetch (fetchImpl) { | |
const store = new Map | |
return url => { | |
const cacheKey = url | |
const cachedContent = store.get(cacheKey) | |
if (cachedContent) { | |
return cachedContent | |
} | |
const noneCacheContent = fetchImpl(url) | |
store.set(cacheKey, noneCacheContent) | |
return noneCacheContent | |
} | |
} | |
const cachedFetch = makeCachedFetch(fetch); | |
console.log(cachedFetch('api/search')); | |
// fetching..., content of .... | |
console.log(cachedFetch('api/search')); | |
// content of search | |
console.log(cachedFetch('api/home')); | |
// fetching... home, content of home | |
console.log(cachedFetch('api/search')); | |
// content of search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment