/** * set the cookie * @param {String} name * @param {String} value * @param {Number} day */ letsetCookie = (name, value, day) => { let date = newDate() date.setDate(date.getDate() + day) document.cookie = name + "=" + value + ";expires=" + date }
/** * get the cookie by name * @param {String} name */ letgetCookie = (name) => { let str = document.cookie.split("; ").filter((i) => { let result = i.split("=") return result[0] == name }) return str.length ? str[0].split("=")[1] : "" }
/** * check user */ letcheckCookie = () => { let username = document.cookie ? getCookie("username") : "" if (username != "") { alert("Welcome " + username) } else { username = prompt("please input your username") if (username && username != "") { setCookie("username", username, 7) } } }
view :: [String] -> IO () view [fileName] = do-- match the filename contents <- readFile fileName -- read the file let todoTasks = lines contents -- get the contents numberedTasks = zipWith (\n line -> show n ++ ": " ++ line) [0..] todoTasks putStrLn $ unlines numberedTasks -- show the items
remove :: [String] -> IO () remove [fileName, numberString] = do-- get the filename and the line number handle <- openFile fileName ReadMode (tempName, tempHandle) <- openTempFile ".""temp"-- create a temp file contents <- hGetContents handle -- get contents from the handle let number = read numberString -- read the String to Number todoTasks = lines contents newTodoTasks = delete (todoTasks !! number) todoTasks -- remove the line of the number hPutStr tempHandle $ unlines newTodoTasks -- putStr into the tempfile hClose handle -- close handles hClose tempHandle removeFile fileName renameFile tempName fileName