lib/unicode/scripts.rb in unicode-scripts-1.10.0 vs lib/unicode/scripts.rb in unicode-scripts-1.11.0
- old
+ new
@@ -44,14 +44,80 @@
res | script_extension_names
}.sort
end
- def self.names(format: :long)
+ def self.augmented_scripts(string)
require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX
- format == :long ?
- INDEX[:SCRIPT_NAMES].sort :
- INDEX[:SCRIPT_ALIASES].keys.sort
+
+ augmented = string.each_codepoint.inject([]){ |res, codepoint|
+ if new_scripts = INDEX[:SCRIPT_EXTENSIONS][codepoint]
+ script_extension_names = new_scripts.map{ |new_script|
+ INDEX[:SCRIPT_ALIASES].key(new_script)
+ }
+ else
+ script_extension_names = scripts([codepoint].pack("U"), format: :short)
+ end
+
+ res | script_extension_names
+ }
+
+ if augmented.include? "Hani"
+ augmented |= ["Hanb", "Jpan", "Kore"]
+ end
+ if augmented.include?("Hira") || augmented.include?("Kana")
+ augmented |= ["Jpan"]
+ end
+ if augmented.include? "Hang"
+ augmented |= ["Kore"]
+ end
+ if augmented.include? "Bopo"
+ augmented |= ["Hanb"]
+ end
+ if augmented.include?("Zyyy") || augmented.include?("Zinh")
+ augmented |= names(format: :short, augmented: :include )
+ end
+
+ augmented.sort
+ end
+
+ def self.resolved_scripts(string)
+ string.chars.reduce(
+ Unicode::Scripts.names(format: :short, augmented: :include)
+ ){ |acc, char|
+ acc & augmented_scripts(char)
+ }
+ end
+
+ def self.mixed?(string)
+ resolved_scripts(string).empty?
+ end
+
+ def self.single?(string)
+ !resolved_scripts(string).empty?
+ end
+
+ # Lists scripts. Options:
+ # - format - :long, :short
+ # - augmented - :include, :exclude, :only
+ def self.names(format: :long, augmented: :exclude)
+ if format == :long && augmented != :exclude
+ raise ArgumentError, "only short four-letter script codes (ISO 15924) supported when listing augmented scripts"
+ end
+
+ if augmented == :only
+ return AUGMENTED_SCRIPT_CODES
+ end
+
+ require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX
+
+ if format == :long
+ INDEX[:SCRIPT_NAMES].sort
+ elsif augmented == :exclude
+ INDEX[:SCRIPT_ALIASES].keys.sort
+ else
+ (INDEX[:SCRIPT_ALIASES].keys + AUGMENTED_SCRIPT_CODES).sort
+ end
end
end
end