captaindavepdx

Untitled

Aug 12th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.26 KB | None | 0 0
  1. //
  2. //  JavaScript.swift
  3. //  SingleFile
  4. //
  5. //  Created by David Littlefield on 8/6/19.
  6. //  Copyright © 2019 David Littlefield. All rights reserved.
  7. //
  8.  
  9. import Cocoa
  10. import Foundation
  11.  
  12. class JavaScript {
  13.    
  14.     static let shared = JavaScript()
  15.    
  16.     private let fileManager = FileManager.default
  17.    
  18.     init() {}
  19.    
  20.     func trim(javaScript: String, fromStart: Bool, inPath: String) {
  21.         let directoryPath = fileManager.currentDirectoryPath
  22.         let filePath = directoryPath + inPath
  23.         let source = try! String(contentsOfFile: filePath, encoding: .utf8)
  24.         let startIndex = source.range(of: javaScript)?.lowerBound ?? source.startIndex
  25.         let newSource = source[startIndex ..< source.endIndex]
  26.         try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
  27.     }
  28.    
  29.     func trim(javaScript: String, toEnd: Bool, inPath: String) {
  30.         let directoryPath = fileManager.currentDirectoryPath
  31.         let filePath = directoryPath + inPath
  32.         let source = try! String(contentsOfFile: filePath, encoding: .utf8)
  33.         let endIndex = source.range(of: javaScript)?.upperBound ?? source.endIndex
  34.         let newSource = source[source.startIndex ..< endIndex]
  35.         try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
  36.     }
  37.    
  38.     func replace(javaScript: String, withJavaScript: String, inPath: String) {
  39.         let directoryPath = fileManager.currentDirectoryPath
  40.         let filePath = directoryPath + inPath
  41.         var source = try! String(contentsOfFile: filePath, encoding: .utf8)
  42.         source = source.replacingOccurrences(of: javaScript, with: withJavaScript)
  43.         try? source.write(toFile: filePath, atomically: true, encoding: .utf8)
  44.     }
  45.    
  46.     func remove(javaScript: String, inPath: String) {
  47.         let directoryPath = fileManager.currentDirectoryPath
  48.         let filePath = directoryPath + inPath
  49.         let source = try! String(contentsOfFile: filePath, encoding: .utf8)
  50.         var lines = source.components(separatedBy: "\n")
  51.         lines = lines.map({ $0.trimmingCharacters(in: .whitespaces) })
  52.         var javaScript = javaScript.components(separatedBy: "\n")
  53.         javaScript = javaScript.map({ $0.trimmingCharacters(in: .whitespaces) })
  54.         for line in javaScript {
  55.             if let index = lines.firstIndex(of: line) {
  56.                 lines.remove(at: index)
  57.             }
  58.         }
  59.         let newSource = lines.joined(separator: "\n")
  60.         try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
  61.     }
  62.    
  63.     func insert(javaScript: String, afterJavaScript: String, inPath: String) {
  64.         let directoryPath = fileManager.currentDirectoryPath
  65.         let filePath = directoryPath + inPath
  66.         let source = try! String(contentsOfFile: filePath, encoding: .utf8)
  67.         var lines = split(javaScript: source, byDelimiter: "\n")
  68.         let javaScript = split(javaScript: javaScript, byDelimiter: "\n")
  69.         let _ = javaScript.compactMap({
  70.             if let i = lines.firstIndex(of: afterJavaScript) { lines.insert($0, at: i + 1)}
  71.         })
  72.         let newSource = lines.joined(separator: "\n")
  73.         try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
  74.     }
  75.    
  76.     func insert(javaScript: String, atEnd:Bool, inPath: String) {
  77.         let directoryPath = fileManager.currentDirectoryPath
  78.         let filePath = directoryPath + inPath
  79.         let source = try! String(contentsOfFile: filePath, encoding: .utf8)
  80.         var lines = split(javaScript: source, byDelimiter: "\n")
  81.         let javaScript = split(javaScript: javaScript, byDelimiter: "\n")
  82.         for line in javaScript { lines.insert(line, at: lines.endIndex) }
  83.         let newSource = lines.joined(separator: "\n")
  84.         try? newSource.write(toFile: filePath, atomically: true, encoding: .utf8)
  85.     }
  86.    
  87.     func split(javaScript: String, byDelimiter: String) -> [String] {
  88.         var lines = javaScript.components(separatedBy: byDelimiter)
  89.         lines = lines.compactMap({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
  90.         return lines
  91.     }
  92.    
  93.     func save(asFileName: String, inPath: String) {
  94.         let directoryPath = fileManager.currentDirectoryPath
  95.         let startFilePath = directoryPath + inPath
  96.         let source = try! String(contentsOfFile: startFilePath)
  97.         let endFilePath = directoryPath + "/" + asFileName
  98.         try? source.write(toFile: endFilePath, atomically: true, encoding: .utf8)
  99.     }
  100.    
  101.     func save(asFileName: String, fromSource: String) {
  102.         let directoryPath = fileManager.currentDirectoryPath
  103.         let filePath = directoryPath + "/" + asFileName
  104.         try? fromSource.write(toFile: filePath, atomically: true, encoding: .utf8)
  105.     }
  106.    
  107.     func removeNewLines(inPath: String) {
  108.         let directoryPath = fileManager.currentDirectoryPath
  109.         let filePath = directoryPath + inPath
  110.         var string = try? String(contentsOfFile: filePath)
  111.         string = string?.replacingOccurrences(of: "\n\n\n\n", with: "\n")
  112.         string = string?.replacingOccurrences(of: "\n\n\n", with: "\n")
  113.         string = string?.replacingOccurrences(of: "\n\n", with: "\n")
  114.         try? string?.write(toFile: filePath, atomically: true, encoding: .utf8)
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment