Giter VIP home page Giter VIP logo

Comments (6)

taflanidi avatar taflanidi commented on August 18, 2024

Hey @AlexisQapa, thanks for your suggestion.
My idea is to add a Pattern Builder entity, allowing things like

let formats: [String] = 
    Pattern("[AAAA]")
        .affineRepeat(min: 3, max: 6, separator: " ")
        .suffix("[000]")
        .build

// produces:

[
    "[AAAA] [AAAA] [AAAA]",
    "[AAAA] [AAAA] [AAAA] [AAAA]",
    "[AAAA] [AAAA] [AAAA] [AAAA] [AAAA]",
    "[AAAA] [AAAA] [AAAA] [AAAA] [AAAA] [AAAA]",
    "[AAAA] [AAAA] [AAAA] [AAAA] [AAAA] [AAAA] [000]",
]

Benefits:

First of all, affine formats allow us to have a consistent complete flag value.

Output                 Complete

1234 1234 123          False
1234 1234 1234         True
1234 1234 1234 1       False
1234 1234 1234 12      False
1234 1234 1234 123     False
1234 1234 1234 1234    True

Second, this Pattern Builder approach doesn't require the library to go full regular expressions syntax, which is good because I hate the idea.

The only thing is to figure out a usable and practical Pattern Builder interface.
Methods like affineRepeat(…) or suffix(…) are just a shot in the dark, I made them up and I can't imagine any new ones at the moment, so tell me your thoughts.

from input-mask-ios.

AlexisQapa avatar AlexisQapa commented on August 18, 2024

Generating all the patterns was my first idea but I didn't knew how to integrate it into the lib. Your builder is definitely a nice solution ! I like how it's just a helper which ppl can opt in opt out

from input-mask-ios.

taflanidi avatar taflanidi commented on August 18, 2024

@AlexisQapa, okay, so let's compose a preliminary interface for this utility.
Once again, I've got no idea what I'm doing, but let's give it a try.

class AffinePattern {
  init(format: String)

  /// Make a [AAAaaa] group
  /// 
  /// - parameter count: number of mandatory letters;
  /// - parameter optionalCount: number of optional letters;
  /// 
  /// - returns: An `AffinePattern` instance.
  class func makeLetters(count: Int, optionalCount: Int = 0) -> AffinePattern

  /// Make a [000999] group
  /// 
  /// - parameter count: number of mandatory digits;
  /// - parameter optionalCount: number of optional digits;
  /// 
  /// - returns: An `AffinePattern` instance.
  class func makeDigits(count: Int, optionalCount: Int = 0) -> AffinePattern

  /// Make a repetitive group.
  /// 
  /// - parameter times: number of repetitions; e.g. [3...5]
  /// - parameter separator: a string to be inserted in between repeated groups;
  /// 
  /// - returns: An `AffinePattern` instance.
  /// 
  /// - usage:
  ///   AffinePattern("[00]").repeat(times: [2..4], separator: " ") // is equal to:
  ///   [
  ///     "[00] [00]",
  ///     "[00] [00] [00]",
  ///     "[00] [00] [00] [00]",
  ///   ]
  func repeat(times: Range<Int>, separator: String = "") -> AffinePattern

  /// For each item in a repetitive group append a suffix.
  /// 
  /// - parameter suffix: a string to be appended
  /// 
  /// - returns: An `AffinePattern` instance.
  /// 
  /// - usage:
  ///   AffinePattern("[00]").repeat(times: [2..4], separator: " ").appending(suffix: "-[0]") // is equal to:
  ///   [
  ///     "[00] [00]-[0]",
  ///     "[00] [00] [00]-[0]",
  ///     "[00] [00] [00] [00]-[0]",
  ///   ]
  func appending(suffix: String) -> AffinePattern

  /// For each item in a repetitive group insert a prefix.
  /// 
  /// - parameter prefix: a string to be inserted
  /// 
  /// - returns: An `AffinePattern` instance.
  /// 
  /// - usage:
  ///   AffinePattern("[00]").repeat(times: [2..4], separator: " ").inserting(prefix: "[0]-") // is equal to:
  ///   [
  ///     "[0]-[00] [00]",
  ///     "[0]-[00] [00] [00]",
  ///     "[0]-[00] [00] [00] [00]",
  ///   ]
  func inserting(prefix: String) -> AffinePattern

  /// For the last item in a repetitive group insert a prefix and append a suffix.
  /// 
  /// - parameter prefix: a string to be inserted
  /// - parameter suffix: a string to be appended
  /// 
  /// - returns: An `AffinePattern` instance.
  /// 
  /// - usage:
  ///   AffinePattern("[00]").repeat(times: [2..4], separator: " "). paddingLast(prefix: "[0]-", suffix: "-[000]") // is equal to:
  ///   [
  ///     "[00] [00]",
  ///     "[00] [00] [00]",
  ///     "[0]-[00] [00] [00] [00]-[000]",
  ///   ]
  func paddingLast(prefix: String, suffix: String) -> AffinePattern

  /// For the first item in a repetitive group insert a prefix and append a suffix.
  /// 
  /// - parameter prefix: a string to be inserted
  /// - parameter suffix: a string to be appended
  /// 
  /// - returns: An `AffinePattern` instance.
  /// 
  /// - usage:
  ///   AffinePattern("[00]").repeat(times: [2..4], separator: " "). paddingFirst(prefix: "[0]-", suffix: "-[000]") // is equal to:
  ///   [
  ///     "[0]-[00] [00]-[000]",
  ///     "[00] [00] [00]",
  ///     "[00] [00] [00] [00]",
  ///   ]
  func paddingFirst(prefix: String, suffix: String) -> AffinePattern

  /// Build a repetitive group
  var build: [String]
}

Anything missing?

from input-mask-ios.

AlexisQapa avatar AlexisQapa commented on August 18, 2024

Seems good to me, I think It would be sufficient for my use case.

I've read that Swift 5 is coming with native support for string regex for your information, might be useful at some point.

from input-mask-ios.

taflanidi avatar taflanidi commented on August 18, 2024

Okay, I'll be adding this new utility in the next library update.

I've read that Swift 5 is coming with native support for string regex for your information, might be useful at some point.

NSRegularExpression.
Not applicable to our library needs, though.

from input-mask-ios.

taflanidi avatar taflanidi commented on August 18, 2024

Hey @AlexisQapa
I wonder if this thing is still needed. Could you please give me a hint?

With the introduction of full-fledged number formatting it seems like this thing might be redundant.

from input-mask-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.