diff --git a/CHANGELOG.md b/CHANGELOG.md index 080df263..d3b54d54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,14 +6,17 @@ All notable changes to this project will be documented in this file. 1. **Collections** - `func parallelizedMap(_ each: @escaping (Self.Iterator.Element) -> R) -> [R?]` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/394) by *Khalian* -2. **Dictionary** +2. **Data** + - `var attributedString` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/379) by *Khalian* + +3. **Dictionary** - `func random() -> Value?` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/420) by *Khalian* -3. **FileManager** +4. **FileManager** - `var documentsDirectoryPath: String?` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/338) by *vilapuigvila* - `var cachesDirectoryPath: String?` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/338) by *vilapuigvila* - -4. **UIStackView** + +5. **UIStackView** - `addArrangedSubviews(_ views: UIView...)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/396) by *kirakik* - `init(distribution: UIStackViewDistribution, alignment: UIStackViewAlignment, axis: UILayoutConstraintAxis, spacing: CGFloat)` in [[PR]](https://github.com/goktugyil/ EZSwiftExtensions/pull/396) by *kirakik* diff --git a/EZSwiftExtensions.xcodeproj/project.pbxproj b/EZSwiftExtensions.xcodeproj/project.pbxproj index 3b3a518f..ef46e336 100644 --- a/EZSwiftExtensions.xcodeproj/project.pbxproj +++ b/EZSwiftExtensions.xcodeproj/project.pbxproj @@ -20,6 +20,8 @@ 5A3292F41DC5BCC50078F7F3 /* CGPointTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3292F31DC5BCC50078F7F3 /* CGPointTests.swift */; }; 5E9795001DDCC80D00E4C959 /* FileManagerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9794FF1DDCC80D00E4C959 /* FileManagerExtension.swift */; }; 5E9795011DDCC80D00E4C959 /* FileManagerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9794FF1DDCC80D00E4C959 /* FileManagerExtension.swift */; }; + 5AB87AC71E50C205008A0B86 /* DataExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AB87AC61E50C205008A0B86 /* DataExtensions.swift */; }; + 5AB87AC91E50C2F4008A0B86 /* DataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AB87AC81E50C2F4008A0B86 /* DataTests.swift */; }; 7605D3121C81FC180046FAC3 /* ArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7605D3021C81FBB80046FAC3 /* ArrayTests.swift */; }; 7605D3131C81FC180046FAC3 /* BlockButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7605D3031C81FBB80046FAC3 /* BlockButtonTests.swift */; }; 7605D3141C81FC180046FAC3 /* BoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7605D3041C81FBB80046FAC3 /* BoolTests.swift */; }; @@ -249,6 +251,8 @@ 5A3292EF1DC5BA6F0078F7F3 /* CGPointExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPointExtensions.swift; sourceTree = ""; }; 5A3292F31DC5BCC50078F7F3 /* CGPointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPointTests.swift; sourceTree = ""; }; 5E9794FF1DDCC80D00E4C959 /* FileManagerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileManagerExtension.swift; sourceTree = ""; }; + 5AB87AC61E50C205008A0B86 /* DataExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataExtensions.swift; sourceTree = ""; }; + 5AB87AC81E50C2F4008A0B86 /* DataTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTests.swift; sourceTree = ""; }; 7605D3021C81FBB80046FAC3 /* ArrayTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayTests.swift; sourceTree = ""; }; 7605D3031C81FBB80046FAC3 /* BlockButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockButtonTests.swift; sourceTree = ""; }; 7605D3041C81FBB80046FAC3 /* BoolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BoolTests.swift; sourceTree = ""; }; @@ -435,6 +439,7 @@ 5A3292F31DC5BCC50078F7F3 /* CGPointTests.swift */, E10A79651D6F65AF00735549 /* CGRectTests.swift */, E10A79661D6F65AF00735549 /* CharacterTests.swift */, + 5AB87AC81E50C2F4008A0B86 /* DataTests.swift */, 7605D3081C81FBB80046FAC3 /* DateTests.swift */, 7605D3061C81FBB80046FAC3 /* DictionaryTests.swift */, E10A79671D6F65AF00735549 /* DoubleTests.swift */, @@ -495,6 +500,7 @@ 5A3292EF1DC5BA6F0078F7F3 /* CGPointExtensions.swift */, B5DC87091C0ED34300972D0A /* CGRectExtensions.swift */, E178781F1C867E9400BC05AA /* CharacterExtensions.swift */, + 5AB87AC61E50C205008A0B86 /* DataExtensions.swift */, E19AC0E81DCBA26500908905 /* DateExtensions.swift */, B5DC870A1C0ED34300972D0A /* DictionaryExtensions.swift */, 31F868DA1C2B6E5E00542250 /* DoubleExtensions.swift */, @@ -744,6 +750,7 @@ E15484E21CA2BCF0003B030E /* ErrorTypeExtensions.swift in Sources */, 31F868DB1C2B6E5E00542250 /* DoubleExtensions.swift in Sources */, FADFAA631D9D8C2F00CF9F7A /* FloatingPointExtensions.swift in Sources */, + 5AB87AC71E50C205008A0B86 /* DataExtensions.swift in Sources */, E19AC0E61DCBA25100908905 /* BundleExtensions.swift in Sources */, B5DC871C1C0ED34300972D0A /* ArrayExtensions.swift in Sources */, E10A79461D6F506400735549 /* UIStoryboardExtensions.swift in Sources */, @@ -848,6 +855,7 @@ E10A79AD1D6F65AF00735549 /* UISliderTests.swift in Sources */, E10A79991D6F65AF00735549 /* UIApplicationTests.swift in Sources */, 7605D3151C81FC180046FAC3 /* CGFloatTests.swift in Sources */, + 5AB87AC91E50C2F4008A0B86 /* DataTests.swift in Sources */, E10A79A11D6F65AF00735549 /* UIColorTests.swift in Sources */, 7605D3181C81FC180046FAC3 /* DateTests.swift in Sources */, E10A79AF1D6F65AF00735549 /* UIStoryboardTests.swift in Sources */, diff --git a/EZSwiftExtensionsTests/DataTests.swift b/EZSwiftExtensionsTests/DataTests.swift new file mode 100644 index 00000000..6d63f696 --- /dev/null +++ b/EZSwiftExtensionsTests/DataTests.swift @@ -0,0 +1,41 @@ +// +// DataTests.swift +// EZSwiftExtensions +// +// Created by Sanyal, Arunav on 2/12/17. +// Copyright © 2017 Goktug Yilmaz. All rights reserved. +// + +import XCTest +import EZSwiftExtensions + +class DataTests: XCTestCase { + + override func setUp() { + super.setUp() + } + + override func tearDown() { + super.tearDown() + } + + func testNSAttributedStringSanityTest() { + let data = Data() + XCTAssertEqual(data.attributedString, NSAttributedString()) + } + + func testNSAttributedStringTest() { + + // We can alternatively store the existing google.com contents on a file + // to stabilize this test case. + let url = URL(string:"http://www.google.com") + do { + let data = try Data(contentsOf: url!) + XCTAssertEqual(data.attributedString?.length, 217) + + // TODO : Write additional tests on attrs, e.g. the font family + } catch _ { + XCTFail() + } + } +} diff --git a/Sources/DataExtensions.swift b/Sources/DataExtensions.swift new file mode 100644 index 00000000..f8efc42e --- /dev/null +++ b/Sources/DataExtensions.swift @@ -0,0 +1,23 @@ +// +// DataExtensions.swift +// EZSwiftExtensions +// +// Created by Sanyal, Arunav on 2/12/17. +// Copyright © 2017 Goktug Yilmaz. All rights reserved. +// + +import Foundation + +extension Data { + + /// EZSE : Attributed string from data + /// Found it here : http://stackoverflow.com/questions/39248092/nsattributedstring-extension-in-swift-3 + public var attributedString: NSAttributedString? { + do { + return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) + } catch let error { + NSLog(error.localizedDescription) + } + return nil + } +}