Skip to content

Commit

Permalink
Create a sanitized space name
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskerr committed Sep 4, 2020
1 parent 46bed5f commit cd0d608
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/js/brim/spaceName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* @flow */

// eslint-disable-next-line
const NON_PRINTABLE = /[\x00-\x09\x0B-\x0C\x0E-\x1F\x7F-\x9F]/g // https://gist.github.com/Hihaj/ba90ccee0cde981655708ce1054f2a7a
const EDGE_SLASH = /^\/|\/$/g
const MID_SLASH = /\//g

export function createSpaceName(input: string) {
const name = input
.replace(NON_PRINTABLE, "")
.replace(EDGE_SLASH, "")
.replace(MID_SLASH, "_")
.trim()

return name.length === 0 ? "Untitled" : name
}
30 changes: 30 additions & 0 deletions src/js/brim/spaceName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* @flow */
import {createSpaceName} from "./spaceName"

test("removes slashes at the front", () => {
expect(createSpaceName("/file")).toEqual("file")
})

test("removes slashes at the end", () => {
expect(createSpaceName("file/")).toEqual("file")
})

test("removes slashes at both sides", () => {
expect(createSpaceName("/file/")).toEqual("file")
})

test("replaces slashes with underscore", () => {
expect(createSpaceName("/file/this/that/cool")).toEqual("file_this_that_cool")
})

test("an emoji", () => {
expect(createSpaceName("🐘")).toBe("🐘")
})

test("empty name", () => {
expect(createSpaceName("")).toBe("Untitled")
})

test("trims space", () => {
expect(createSpaceName(" name ")).toBe("name")
})
5 changes: 3 additions & 2 deletions src/js/flows/createSubspace.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* @flow */
import type {Thunk} from "../state/types"
import {createSpaceName} from "../brim/spaceName"
import {getUniqName} from "../lib/uniqName"
import {getZealot} from "./getZealot"
import Current from "../state/Current"
import Spaces from "../state/Spaces"
import Tabs from "../state/Tabs"
import Viewer from "../state/Viewer"
import brim from "../brim"
import refreshSpaceNames from "./refreshSpaceNames"
import {getZealot} from "./getZealot"

class CreateSubspaceError extends Error {}

Expand All @@ -23,7 +24,7 @@ export default (): Thunk => (dispatch, getState) => {
try {
const logs = records.map((r) => r.mustGet("_log").stringValue())
const keys = records.map((r) => r.mustGet("key").stringValue())
const name = getUniqName(keys[0], names)
const name = getUniqName(createSpaceName(keys[0]), names)
return zealot.subspaces
.create({name, spaceId, logs})
.then((space) =>
Expand Down

0 comments on commit cd0d608

Please sign in to comment.