From 5f4611d65e40eae3ca6191a15f68d69ea5a1c4cb Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Tue, 20 Jul 2021 21:24:52 +0300 Subject: WIP --- app/utils/delay.ts | 7 ---- app/utils/ignore-warnings.ts | 10 ----- app/utils/keychain.ts | 67 ------------------------------ app/utils/storage/index.ts | 1 - app/utils/storage/storage.test.ts | 43 -------------------- app/utils/storage/storage.ts | 79 ------------------------------------ app/utils/validate.ts | 85 --------------------------------------- 7 files changed, 292 deletions(-) delete mode 100644 app/utils/delay.ts delete mode 100644 app/utils/ignore-warnings.ts delete mode 100644 app/utils/keychain.ts delete mode 100644 app/utils/storage/index.ts delete mode 100644 app/utils/storage/storage.test.ts delete mode 100644 app/utils/storage/storage.ts delete mode 100644 app/utils/validate.ts (limited to 'app/utils') diff --git a/app/utils/delay.ts b/app/utils/delay.ts deleted file mode 100644 index 66c404f..0000000 --- a/app/utils/delay.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * A "modern" sleep statement. - * - * @param ms The number of milliseconds to wait. - */ -export const delay = (ms: number) => - new Promise((resolve) => setTimeout(resolve, ms)) diff --git a/app/utils/ignore-warnings.ts b/app/utils/ignore-warnings.ts deleted file mode 100644 index 3d02efd..0000000 --- a/app/utils/ignore-warnings.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Ignore some yellowbox warnings. Some of these are for deprecated functions - * that we haven't gotten around to replacing yet. - */ -import { LogBox } from 'react-native' - -// prettier-ignore -LogBox.ignoreLogs([ - "Require cycle:", -]) diff --git a/app/utils/keychain.ts b/app/utils/keychain.ts deleted file mode 100644 index 86fae6f..0000000 --- a/app/utils/keychain.ts +++ /dev/null @@ -1,67 +0,0 @@ -import * as ReactNativeKeychain from 'react-native-keychain' - -/** - * Saves some credentials securely. - * - * @param username The username - * @param password The password - * @param server The server these creds are for. - */ -export async function save( - username: string, - password: string, - server?: string, -) { - if (server) { - await ReactNativeKeychain.setInternetCredentials(server, username, password) - return true - } else { - return ReactNativeKeychain.setGenericPassword(username, password) - } -} - -/** - * Loads credentials that were already saved. - * - * @param server The server that these creds are for - */ -export async function load(server?: string) { - if (server) { - const creds = await ReactNativeKeychain.getInternetCredentials(server) - return { - username: creds ? creds.username : null, - password: creds ? creds.password : null, - server, - } - } else { - const creds = await ReactNativeKeychain.getGenericPassword() - if (typeof creds === 'object') { - return { - username: creds.username, - password: creds.password, - server: null, - } - } else { - return { - username: null, - password: null, - server: null, - } - } - } -} - -/** - * Resets any existing credentials for the given server. - * - * @param server The server which has these creds - */ -export async function reset(server?: string) { - if (server) { - await ReactNativeKeychain.resetInternetCredentials(server) - return true - } else { - const result = await ReactNativeKeychain.resetGenericPassword() - return result - } -} diff --git a/app/utils/storage/index.ts b/app/utils/storage/index.ts deleted file mode 100644 index 69b61ec..0000000 --- a/app/utils/storage/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './storage' diff --git a/app/utils/storage/storage.test.ts b/app/utils/storage/storage.test.ts deleted file mode 100644 index b8cf732..0000000 --- a/app/utils/storage/storage.test.ts +++ /dev/null @@ -1,43 +0,0 @@ -import AsyncStorage from '@react-native-async-storage/async-storage' -import { load, loadString, save, saveString, clear, remove } from './storage' - -// fixtures -const VALUE_OBJECT = { x: 1 } -const VALUE_STRING = JSON.stringify(VALUE_OBJECT) - -beforeEach(() => - (AsyncStorage.getItem as jest.Mock).mockReturnValue( - Promise.resolve(VALUE_STRING), - ), -) -afterEach(() => jest.clearAllMocks()) - -test('load', async () => { - const value = await load('something') - expect(value).toEqual(JSON.parse(VALUE_STRING)) -}) - -test('loadString', async () => { - const value = await loadString('something') - expect(value).toEqual(VALUE_STRING) -}) - -test('save', async () => { - await save('something', VALUE_OBJECT) - expect(AsyncStorage.setItem).toHaveBeenCalledWith('something', VALUE_STRING) -}) - -test('saveString', async () => { - await saveString('something', VALUE_STRING) - expect(AsyncStorage.setItem).toHaveBeenCalledWith('something', VALUE_STRING) -}) - -test('remove', async () => { - await remove('something') - expect(AsyncStorage.removeItem).toHaveBeenCalledWith('something') -}) - -test('clear', async () => { - await clear() - expect(AsyncStorage.clear).toHaveBeenCalledWith() -}) diff --git a/app/utils/storage/storage.ts b/app/utils/storage/storage.ts deleted file mode 100644 index 659e738..0000000 --- a/app/utils/storage/storage.ts +++ /dev/null @@ -1,79 +0,0 @@ -import AsyncStorage from '@react-native-async-storage/async-storage' - -/** - * Loads a string from storage. - * - * @param key The key to fetch. - */ -export async function loadString(key: string): Promise { - try { - return await AsyncStorage.getItem(key) - } catch { - // not sure why this would fail... even reading the RN docs I'm unclear - return null - } -} - -/** - * Saves a string to storage. - * - * @param key The key to fetch. - * @param value The value to store. - */ -export async function saveString(key: string, value: string): Promise { - try { - await AsyncStorage.setItem(key, value) - return true - } catch { - return false - } -} - -/** - * Loads something from storage and runs it thru JSON.parse. - * - * @param key The key to fetch. - */ -export async function load(key: string): Promise { - try { - const almostThere = await AsyncStorage.getItem(key) - return JSON.parse(almostThere) - } catch { - return null - } -} - -/** - * Saves an object to storage. - * - * @param key The key to fetch. - * @param value The value to store. - */ -export async function save(key: string, value: any): Promise { - try { - await AsyncStorage.setItem(key, JSON.stringify(value)) - return true - } catch { - return false - } -} - -/** - * Removes something from storage. - * - * @param key The key to kill. - */ -export async function remove(key: string): Promise { - try { - await AsyncStorage.removeItem(key) - } catch {} -} - -/** - * Burn it all to the ground. - */ -export async function clear(): Promise { - try { - await AsyncStorage.clear() - } catch {} -} diff --git a/app/utils/validate.ts b/app/utils/validate.ts deleted file mode 100644 index 862b2d0..0000000 --- a/app/utils/validate.ts +++ /dev/null @@ -1,85 +0,0 @@ -const ValidateJS = require('validate.js') - -// HACK(steve): wierd typescript situation because of strange typings -const Validate: any = ValidateJS.default ? ValidateJS.default : ValidateJS - -/** - * Validates that 1 attribute doesn't appear in another's attributes content. - */ -Validate.validators.excludes = function custom( - value, - options, - key, - attributes, -) { - const list = attributes[options.attribute] || [] - if (value && list.includes(value)) { - return options.message || `${value} is in the list` - } -} - -/** - * Validates that another attribute isn't true. - */ -Validate.validators.tripped = function custom(value, options, key, attributes) { - if (value && attributes[options.attribute] === true) { - return options.message || `${options.attribute} is true` - } -} - -/** - * Defines the rules for validating. - * - * Example: - * ```ts - * const RULES = { - * favoriteBand: { - * inclusion: { ['Weezer', 'Other'], message: 'Pick wisely.' } - * }, - * name: { - * presence: { message: 'A developer has no name?' } - * } - * } - * validate(RULES, {}) - * ``` - * - * See https://validatejs.org/#validators for more examples. - * - */ -export interface ValidationRules { - [key: string]: Record -} - -/** - * An object containing any errors found. - * - * Example: - * ```js - * { - * email: ['Invalid email address.'], - * password: [ - * 'Password must be 6 characters.', - * 'Password must have at least 1 digit.' - * ] - * } - * ``` - */ -export interface ValidationErrors { - [key: string]: string[] -} - -/** - * Runs the given rules against the data object. - * - * @param rules The rules to apply. - * @param data The object to validate. - */ -export function validate( - rules: ValidationRules, - data: Record, -): ValidationErrors { - if (typeof data !== 'object') { - return {} as ValidationErrors - } - return Validate(data, rules, { fullMessages: false }) || {} -} -- cgit v1.2.3