Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // JavaScript.swift
- // SingleFile
- //
- // Created by David Littlefield on 8/6/19.
- // Copyright © 2019 David Littlefield. All rights reserved.
- //
- import Cocoa
- import Foundation
- class JavaScript {
- static let shared = JavaScript()
- private let fileManager = FileManager.default
- init() {}
- func trim(javaScript: String, fromStart: Bool, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- let source = try! String(contentsOfFile: filePath, encoding: .utf8)
- let startIndex = source.range(of: javaScript)?.lowerBound ?? source.startIndex
- let newSource = source[startIndex ..< source.endIndex]
- try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func trim(javaScript: String, toEnd: Bool, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- let source = try! String(contentsOfFile: filePath, encoding: .utf8)
- let endIndex = source.range(of: javaScript)?.upperBound ?? source.endIndex
- let newSource = source[source.startIndex ..< endIndex]
- try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func replace(javaScript: String, withJavaScript: String, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- var source = try! String(contentsOfFile: filePath, encoding: .utf8)
- source = source.replacingOccurrences(of: javaScript, with: withJavaScript)
- try? source.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func remove(javaScript: String, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- let source = try! String(contentsOfFile: filePath, encoding: .utf8)
- var lines = source.components(separatedBy: "\n")
- lines = lines.map({ $0.trimmingCharacters(in: .whitespaces) })
- var javaScript = javaScript.components(separatedBy: "\n")
- javaScript = javaScript.map({ $0.trimmingCharacters(in: .whitespaces) })
- for line in javaScript {
- if let index = lines.firstIndex(of: line) {
- lines.remove(at: index)
- }
- }
- let newSource = lines.joined(separator: "\n")
- try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func insert(javaScript: String, afterJavaScript: String, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- let source = try! String(contentsOfFile: filePath, encoding: .utf8)
- var lines = split(javaScript: source, byDelimiter: "\n")
- let javaScript = split(javaScript: javaScript, byDelimiter: "\n")
- let _ = javaScript.compactMap({
- if let i = lines.firstIndex(of: afterJavaScript) { lines.insert($0, at: i + 1)}
- })
- let newSource = lines.joined(separator: "\n")
- try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func insert(javaScript: String, atEnd:Bool, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- let source = try! String(contentsOfFile: filePath, encoding: .utf8)
- var lines = split(javaScript: source, byDelimiter: "\n")
- let javaScript = split(javaScript: javaScript, byDelimiter: "\n")
- for line in javaScript { lines.insert(line, at: lines.endIndex) }
- let newSource = lines.joined(separator: "\n")
- try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func split(javaScript: String, byDelimiter: String) -> [String] {
- var lines = javaScript.components(separatedBy: byDelimiter)
- lines = lines.compactMap({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
- return lines
- }
- func save(asFileName: String, inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let startFilePath = directoryPath + inPath
- let source = try! String(contentsOfFile: startFilePath)
- let endFilePath = directoryPath + "/" + asFileName
- try? source.write(toFile: endFilePath, atomically: true, encoding: .utf8)
- }
- func save(asFileName: String, fromSource: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + "/" + asFileName
- try? fromSource.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- func removeNewLines(inPath: String) {
- let directoryPath = fileManager.currentDirectoryPath
- let filePath = directoryPath + inPath
- var string = try? String(contentsOfFile: filePath)
- string = string?.replacingOccurrences(of: "\n\n\n\n", with: "\n")
- string = string?.replacingOccurrences(of: "\n\n\n", with: "\n")
- string = string?.replacingOccurrences(of: "\n\n", with: "\n")
- try? string?.write(toFile: filePath, atomically: true, encoding: .utf8)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment