Merge pull request #704 from antonva/case-insensitive-containers

Container commands exists and fuzzyMatch are now case insensitive.
This commit is contained in:
Oliver Blanthorn 2018-06-19 15:26:03 +01:00 committed by GitHub
commit 6ffdc59c1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 48 deletions

View file

@ -1796,20 +1796,23 @@ export async function containerclose(name: string) {
)
})
}
/** Creates a new container.
/** Creates a new container. Note that container names must be unique and that the checks are case-insensitive.
Further reading https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextualIdentities/ContextualIdentity
Further reading https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/contextualIdentities/ContextualIdentity
@param name The container name. Must be unique.
@param color The container color. Valid colors are: "blue", "turquoise", "green", "yellow", "orange", "red", "pink", "purple". If no color is chosen a random one will be selected from the list of valid colors.
@param icon The container icon. Valid icons are: "fingerprint", "briefcase", "dollar", "cart", "circle", "gift", "vacation", "food", "fruit", "pet", "tree", "chill". If no icon is chosen, it defaults to "fingerprint".
Example usage:
- `:containercreate tridactyl green dollar`
@param name The container name. Must be unique.
@param color The container color. Valid colors are: "blue", "turquoise", "green", "yellow", "orange", "red", "pink", "purple". If no color is chosen a random one will be selected from the list of valid colors.
@param icon The container icon. Valid icons are: "fingerprint", "briefcase", "dollar", "cart", "circle", "gift", "vacation", "food", "fruit", "pet", "tree", "chill". If no icon is chosen, it defaults to "fingerprint".
*/
//#background
export async function containercreate(name: string, color?: string, icon?: string) {
await Container.create(name, color, icon)
}
/** Delete a container. Closes all tabs associated with that container beforehand.
/** Delete a container. Closes all tabs associated with that container beforehand. Note: container names are case-insensitive.
@param name The container name.
*/
//#background
@ -1818,15 +1821,15 @@ export async function containerremove(name: string) {
await Container.remove(name)
}
/** Update a container's information. Note that none of the parameters are optional.
/** Update a container's information. Note that none of the parameters are optional and that container names are case-insensitive.
Example usage:
- Changing the container name: `containerupdate banking blockchain green dollar`
- Changing the container name: `:containerupdate banking blockchain green dollar`
- Changing the container icon: `containerupdate banking banking green briefcase`
- Changing the container icon: `:containerupdate banking banking green briefcase`
- Changing the container color: `containerupdate banking banking purple dollar`
- Changing the container color: `:containerupdate banking banking purple dollar`
@param name The container name.
@param uname The new container name. Must be unique.

View file

@ -30,9 +30,9 @@ const ContainerIcon = [
]
/** Creates a container from the specified parameters.Does not allow multiple containers with the same name.
* @param name The container name.
* @param color The container color, must be one of: "blue", "turquoise", "green", "yellow", "orange", "red", "pink" or "purple". If nothing is supplied, it selects one at random.
* @param icon The container icon, must be one of: "fingerprint", "briefcase", "dollar", "cart", "circle", "gift", "vacation", "food", "fruit", "pet", "tree", "chill"
@param name The container name.
@param color The container color, must be one of: "blue", "turquoise", "green", "yellow", "orange", "red", "pink" or "purple". If nothing is supplied, it selects one at random.
@param icon The container icon, must be one of: "fingerprint", "briefcase", "dollar", "cart", "circle", "gift", "vacation", "food", "fruit", "pet", "tree", "chill"
*/
export async function create(
name: string,
@ -63,7 +63,7 @@ export async function create(
}
/** Removes specified container. No fuzzy matching is intentional here. If there are multiple containers with the same name (allowed by other container plugins), it chooses the one with the lowest cookieStoreId
* @param name The container name
@param name The container name
*/
export async function remove(name: string) {
logger.debug(name)
@ -77,12 +77,12 @@ export async function remove(name: string) {
}
/** Updates the specified container.
* TODO: pass an object to this when tridactyl gets proper flag parsing
* NOTE: while browser.contextualIdentities.create does check for valid color/icon combos, browser.contextualIdentities.update does not.
* @param containerId Expects a cookieStringId e.g. "firefox-container-n".
* @param name the new name of the container
* @param color the new color of the container
* @param icon the new icon of the container
TODO: pass an object to this when tridactyl gets proper flag parsing
NOTE: while browser.contextualIdentities.create does check for valid color/icon combos, browser.contextualIdentities.update does not.
@param containerId Expects a cookieStringId e.g. "firefox-container-n".
@param name the new name of the container
@param color the new color of the container
@param icon the new icon of the container
*/
export async function update(
containerId: string,
@ -105,7 +105,7 @@ export async function update(
}
/** Gets a container object from a supplied container id string.
* @param containerId Expects a cookieStringId e.g. "firefox-container-n"
@param containerId Expects a cookieStringId e.g. "firefox-container-n"
*/
export async function getFromId(containerId: string): Promise<{}> {
try {
@ -115,14 +115,18 @@ export async function getFromId(containerId: string): Promise<{}> {
}
}
/** Queries Firefox's contextual identities API for a container with a specific name.
* @param string cname
* @returns boolean Returns true when cname matches an existing container or on query error.
/** Fetches all containers from Firefox's contextual identities API and checks if one exists with the specified name.
Note: This operation is entirely case-insensitive.
@param string cname
@returns boolean Returns true when cname matches an existing container or on query error.
*/
export async function exists(cname: string): Promise<boolean> {
let exists = false
try {
let res = await browser.contextualIdentities.query({ name: cname })
let containers = await getAll()
let res = containers.filter(c => {
return c.name.toLowerCase() === cname
})
if (res.length > 0) {
exists = true
}
@ -137,10 +141,10 @@ export async function exists(cname: string): Promise<boolean> {
}
/** Takes string parameters and returns them as a pseudo container object
* for use in other functions in the library.
* @param name
* @param color
* @param icon
for use in other functions in the library.
@param name
@param color
@param icon
*/
export function fromString(name: string, color: string, icon: string) {
try {
@ -178,12 +182,15 @@ export async function getId(name: string): Promise<string> {
}
/** Tries some simple ways to match containers to your input.
* @param partialName The (partial) name of the container.
Fuzzy matching is entirely case-insensitive.
@param partialName The (partial) name of the container.
*/
export async function fuzzyMatch(partialName: string): Promise<string> {
let exactMatch = await browser.contextualIdentities.query({
name: partialName,
let containers = await getAll()
let exactMatch = containers.filter(c => {
return c.name.toLowerCase() === partialName
})
if (exactMatch.length === 1) {
return exactMatch[0]["cookieStoreId"]
} else if (exactMatch.length > 1) {
@ -191,14 +198,9 @@ export async function fuzzyMatch(partialName: string): Promise<string> {
"[Container.fuzzyMatch] more than one container with this name exists.",
)
} else {
let fuzzyMatches = []
let containers = await getAll()
for (let c of containers) {
if (c["name"].indexOf(partialName) === 0) {
// Only match start of name.
fuzzyMatches.push(c)
}
}
let fuzzyMatches = containers.filter(c => {
return c.name.toLowerCase().indexOf(partialName) > -1
})
if (fuzzyMatches.length === 1) {
return fuzzyMatches[0]["cookieStoreId"]
} else if (fuzzyMatches.length > 1) {
@ -221,15 +223,9 @@ function chooseRandomColor(): string {
}
function isValidColor(color: string): boolean {
for (let c of ContainerColor) {
if (c === color) return true
}
return false
return ContainerColor.indexOf(color) > -1
}
function isValidIcon(icon: string): boolean {
for (let i of ContainerIcon) {
if (i === icon) return true
}
return false
return ContainerIcon.indexOf(icon) > -1
}